【问题标题】:php, mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statementphp, mysqli_stmt::bind_param() [mysqli-stmt.bind-param]:变量的数量与准备好的语句中的参数数量不匹配
【发布时间】:2011-01-18 22:10:13
【问题描述】:

我有以下代码:

$id = $_GET['id'];

// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM date WHERE id=?"))
{
        $stmt->bind_param("isssssssss", $id, $mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email);
        $stmt->execute();

        $stmt->bind_result($id, $mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email);
        $stmt->fetch();

        // show the form
        renderForm($mtcn, $amount, $currency, $sender_name, $sender_country, $receiver_name, $comment, $support, $email, NULL, $id);

        $stmt->close();
}
// show an error if the query has an error
else
{
        echo "Error: could not prepare SQL statement";
}

会产生以下错误/警告:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in C:\wamp\www\records.php on line 143

Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: (HY000/2031): No data supplied for parameters in prepared statement in C:\wamp\www\records.php on line 144

Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement in C:\wamp\www\records.php on line 146

Warning: mysqli_stmt::fetch() [mysqli-stmt.fetch]: (HY000/2053): Attempt to read a row while there is no result set associated with the statement in C:\wamp\www\records.php on line 147

表格并没有像我预期的那样使用来自 db 的数据完成......任何建议/帮助将不胜感激。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您误解了语句的使用。

    在你的查询语句中,你只需要绑定一个参数,id。

    $stmt->bind_param("i", $id);
    $id = 5; // fetch the fifth record
    $stmt->execute();
    

    然后您通过获取结果对象来访问查询结果中的值。

    查看 mysqli_stmt::execute 示例,php.net

    【讨论】:

    • 我仍然收到警告:mysqli_stmt::bind_result() [mysqli-stmt.bind-result]:绑定变量的数量与 C:\wamp 中准备好的语句中的字段数量不匹配\www\records.php 第 146 行。但从我的行中,我看到变量的数量与字段的数量相匹配。我错过了什么吗?
    • 尝试用您要检索的确切字段列表替换您的“SELECT *”。
    猜你喜欢
    • 2013-01-14
    • 2016-01-23
    • 2023-03-27
    • 2015-05-17
    • 2013-05-27
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多