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;.

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

报告相同问题?