arianhojat Posted May 10, 2007 Share Posted May 10, 2007 Hello all, I asked this question 3 times in PHP Help forum but no one answered, so hoping maybe a MYSQL guru who uses php might know better... Even though this is more php related, i dont want to post same topic over there again when someone over here might be more knowledgable... Had a few questions regarding sprintf versus prepared statements to protect a query from injection etc. Here are my test examples that my questions are based off of... ##### sprintf example <?php function secure_query($var)//returns variable for database insertion { if (!is_numeric($var))//string return "'". mysql_real_escape_string($var) ."'"; //basically adds slashes to strings, (heard this is betetr than addslashes() as its custom to mysql's current char set ) else return $var;//number } $query = sprintf ( "UPDATE theDB.table SET Description=%s, Comments=%s, age=%i WHERE id=%d", , secure_query($description), (($comments!="") ? secure_query($comments) : 'NULL' ), (($age!="") ? secure_query($age) : 'NULL' ), secure_query($id) ); ?> #####prepared example <?php if ($stmt = $mysqli->prepare("UPDATE theDB.table SET Description=?, Comments=?, age=? WHERE id=?")) { $stmt->bind_param("s", $description ); $stmt->bind_param("s", (($comments!="") ? $comments : 'NULL' ) ); $stmt->bind_param("i", (($age!="") ? $age : 'NULL' ) ); $stmt->bind_param("i", $id ); $stmt->execute(); $stmt->close(); } ?> 1. Is a prepared statement in php safer than say a normal query with formatted with sprintf? what are advantages/dis to each if there are any and what do you prefer? (i know prepared statements are faster if doing many of same query and i beleive dont need to addslashes to the variables you pass it since it does that builtin.). 2. my $comments and $age variables can either have string(or int data respectively) but if those textfields pretend were filled blank by the user, i want to put NULL in the database. Since I can pass an string/integer or 'NULL' to sprintf in those variables, it seems like sprintf will definately not like that 'NULL' is not an integer for example. so would this be a reason not to use sprintf? i guess you could always get passed it by doing... sprintf ( "UPDATE theDB.table SET Description=%s, Comments=%s, age=".( ($age==NULL)?'%s':'%d')." WHERE id=%d", secure_query($description), (($comments!="") ? secure_query($comments) : 'NULL' ), (($age!="") ? secure_query($age) : 'NULL' ), secure_query($id) ); What would you do in this situation, use prepared statement instead? Thanks in advance guys! Link to comment https://forums.phpfreaks.com/topic/50856-protect-against-sql-injection-prepared-statements-versus-formatted-string/ Share on other sites More sharing options...
bubblegum.anarchy Posted May 11, 2007 Share Posted May 11, 2007 The end result of either a `prepared query` or a `sprintf query` is exactly the same... a string... The formatting of the query string passed to mysql is what is important. Quote strings and type define numbers so that there is not chance a quoted string will be passed as a number. Link to comment https://forums.phpfreaks.com/topic/50856-protect-against-sql-injection-prepared-statements-versus-formatted-string/#findComment-250238 Share on other sites More sharing options...
btherl Posted May 11, 2007 Share Posted May 11, 2007 It's a matter of style really.. I use #2, but I don't use sprintf(), I just concatenate the query together placing the variables in place. $powerpuff = mysql_real_escape_string($powerpuff); $sql = 'SELECT foo ' . ' FROM bar ' . ' WHERE bubbles =\'' . $powerpuff_esc . '\''; Something like that.. it's quite readable in an editor with highlighting. I would go for another approach if highlighting was not available though, as it looks yucky without. Link to comment https://forums.phpfreaks.com/topic/50856-protect-against-sql-injection-prepared-statements-versus-formatted-string/#findComment-250253 Share on other sites More sharing options...
arianhojat Posted May 11, 2007 Author Share Posted May 11, 2007 btherl, yeh i use that method but maybe i try switching it up with prepared queries now for repeated INSERTs/other queries. Link to comment https://forums.phpfreaks.com/topic/50856-protect-against-sql-injection-prepared-statements-versus-formatted-string/#findComment-250283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.