dongyishen5796 2016-08-09 10:20
浏览 122
已采纳

MySQL - 绑定变量的数量与令牌的数量不匹配

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
  • 写回答

1条回答 默认 最新

  • dongque20030402 2016-08-09 10:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?