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]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?