Thursday, 15 March 2012

Using one ? to contain several variables in PHP MySQLi prepared INSERT statement -



Using one ? to contain several variables in PHP MySQLi prepared INSERT statement -

trying grips prepared statements insert query. supposed add together new user database:

$statement = $mysqli->prepare("insert users (email,passwordhash) values (?)"); $statement->bind_param('s', "'$email','$passwordhash'"); $statement->execute();

is right utilize single ? , fill 2 values in way?

the way mysqli doing need bind variables separately

$statement = $mysqli->prepare("insert users (email,passwordhash) values (?,?)"); $statement->bind_param('ss', $email,$passwordhash); $statement->execute();

but if want way (say, have array ready , want insert using 1 placeholder) need helper class translate custom placeholder right sql statement:

$data = array('email'=>$email, 'passwordhash'=>$passwordhash); $db->query("insert users set ?u");

and shorter raw mysqli yet much more - error handling, profiling , such.

also maintain in mind when have variable number of fields insert, mysqli turned nightmare.

php insert mysqli

No comments:

Post a Comment