Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2010-01-19 14:17:23

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Prepared statements

Opinion time. smile If designing something from scratch, would you stick with just using sql_escaped strings or go down the prepared statement avenue too? Does preparing the statements introduce a notable overhead?


Screw the chavs and God save the Queen!

Offline

#2 2010-01-19 14:25:33

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,071
Website

Re: Prepared statements

No overhead that I know of. If you can, go for prepared statements. They are much safer, and you can't even forget about the security precaution if you use them...


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#3 2010-01-19 14:30:37

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: Prepared statements

Cheers. smile I'll get some revision done on those then. I've very few queries in existence at the moment, so it's the prime time to add them. Is it best, (as an extra level of caution), to still escape strings if using prepared statements, or should that habit be dropped when using prepared statements?


Screw the chavs and God save the Queen!

Offline

#4 2010-01-19 14:39:15

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 4,071
Website

Re: Prepared statements

That would result in you having escaped strings in the database.

If I understood prepared statements correctly, the strings or whatever parameters you hand over are sent separated from the actual query to the database, and only there they are used to replace the placeholders - after parsing the query.

Actually, an interesting question that brings up for me: Is it possible to use the placeholder in combination with some arithmetic operation -  something like INSERT INTO foo (bar) VALUES (?+2). This is obviously a pointless example, but a more realistic one could be adding the result of some SELECT subquery.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#5 2010-01-19 14:49:19

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: Prepared statements

lie2815 wrote:

If I understood prepared statements correctly, the strings or whatever parameters you hand over are sent separated from the actual query to the database, and only there they are used to replace the placeholders - after parsing the query.

That makes things a bit clearer. I'd not figured the way they worked yet. big_smile So literally the values just get dumped in the DB wherever the respective placeholders are, and no processing is done with the strings as such in any other regard.


Actually, an interesting question that brings up for me: Is it possible to use the placeholder in combination with some arithmetic operation -  something like INSERT INTO foo (bar) VALUES (?+2). This is obviously a pointless example, but a more realistic one could be adding the result of some SELECT subquery.

I'll let you know what I find out on that one. I get the feeling I've got a good few hours of reading in front of me, switching over to these, so I'll quite likely find the answer to that scenario. big_smile


Screw the chavs and God save the Queen!

Offline

#6 2010-01-19 17:29:00

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: Prepared statements

Just been playing around with the code, and the likes of what you mention shouldn't be a problem, due to the way the queries/statements are written. Still not 100 percent certain yet, but I think it should be straight forward enough. It's taking some getting used to though. This is the basic code I've been running just to test the layer.

$db->prepare('dbtest', 'SELECT email FROM users WHERE username=$1 AND username!=$2');
$db->execute('dbtest', array('Spider', 'Guest'));
$info = $db->result();

Should be simple enough to adapt that example to allow and mix and match of sub queries, etc. Just spent ages trying to figure out why things weren't working, btw. Eventually realised that I'd forgotten to assign the result resource in the DB layer for the execute. The simplest of things... big_smile

Last edited by MattF (2010-01-19 20:07:41)


Screw the chavs and God save the Queen!

Offline

#7 2010-01-20 00:35:34

MattF
Member
From: South Yorkshire, England
Registered: 2008-05-06
Posts: 1,230
Website

Re: Prepared statements

Must say, one other notable benefit of this is that it does make the code look so much cleaner and easier to read too. It's not half as bad to read through and check for syntax. Plus, using query_params on the simple statements makes it a fairly straight forward enough replacement for query.


Screw the chavs and God save the Queen!

Offline

Board footer

Powered by FluxBB 1.5.0