dongyu1983 2015-12-29 21:27
浏览 75
已采纳

SQL LIMIT:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

I am using PDO to retrieve data from php. I am trying to use limit function but sadly it did not work. I searched around the forum for similar questions and found answers similar to what I tried below. But I get the same warning. I am relatively new to PDO. Am I doing something wrong?

$limit = 5;
$users = $db->prepare("SELECT code,name from Portion where name LIKE '%$t%' LIMIT :limit");
$users->bindParam(':limit', $limit, PDO::PARAM_INT);
$users->execute(['query' => "{$_GET['query']}%"]);
  • 写回答

1条回答 默认 最新

  • dongqiao5573 2015-12-29 21:40
    关注

    When you want to fill in parameters of a PDO query, you either

    • Use bindParam() or bindValue() to bind them before calling execute(), or
    • Provide an array of values as an argument to execute().

    You can't mix them -- when you supply the array argument, that overrides the bindParam settings. Since you're passing an array argument to execute() (although for no apparent reason, since there's no :query parameter in the SQL), the :limit parameter is being lost.

    Change your code to:

    $limit = 5;
    $users = $db->prepare("SELECT code,name from Portion where name LIKE CONCAT('%', :pattern, '%') LIMIT :limit");
    $users->execute([':pattern' => $t, ':limit' => $limit]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?