doufen3786 2013-09-07 04:35
浏览 39
已采纳

绑定参数到mysqli数据库查询

I'm getting this:

Warning: mysqli_stmt_bind_result() [function.mysqli-stmt-bind-result]: Number of bind variables doesn't match number of fields in prepared statement

I've defined $id and $link above this in my code, and have no trouble connecting to the database.

$query = "select * from tablename where id = ?";

if ($stmt = mysqli_prepare($link, $query)) {
    mysqli_stmt_bind_param($stmt, 'i', $id);
    //execute statement
    mysqli_stmt_execute($stmt);
    // bind result variables
    mysqli_stmt_bind_result($stmt, $first, $last);
    // fetch values
    mysqli_stmt_fetch($stmt);
    echo "$first $last<br>";
    // close statement
    mysqli_stmt_close($stmt);
}

It seems to me that the query only has one '?', so it should only need one (integer typed) value to fill in, which I think I'm supplying with $id. Help?

  • 写回答

1条回答 默认 最新

  • dongquming3255 2013-09-07 04:38
    关注

    You have to put each column name in your SELECT rather than use the * wildcard, Documentation.

    Example

    SELECT `FirstName`,`LastName` FROM `tablename` WHERE `id` = ?
    

    The error said Number of bind variables doesn't match number of fields in prepared statement.

    In line mysqli_stmt_bind_result($stmt, $first, $last); you're trying to bind two variables ($first and $last) to $stmt. But the amount of selected variables from the database has to be equal to the amount of variables you're trying to assing to $stmt. You can do that as stated above.

    Side Note: I recommend quoted &grave;tables&grave; and &grave;columns&grave; names with backticks &grave;.

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

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体