daninthemix daninthemix - 3 months ago 9
MySQL Question

MySQL - number of bound variables does not match number of tokens

Can't figure out why this code isn't working:

$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([$SQL_values]);


And these are dumps of the two strings being inserted into those statements:

$SQL_update = UPDATE laptops SET asset_tag = :asset_tag WHERE id = :id
$SQL_values = 'asset_tag' => 5544, 'id' => 23

Answer

You missed : in your code:-

$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([':asset_tag' => 5544, ':id' => 23]);

So actually what you have to do is:-

$SQL_values =[':asset_tag' => 5544, ':id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array

Or

$SQL_values =['asset_tag' => 5544, 'id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array

Note:- execute won't accept a string, it must be an array.

Comments