【问题标题】:php mysql bind param parametersphp mysql绑定参数参数
【发布时间】:2014-12-06 01:56:45
【问题描述】:

我第一次尝试使用准备好的语句,并在下面的代码中遇到了以下问题

错误:

警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,布尔值

代码:

$stmt = mysqli_prepare($db, "INSERT INTO fragrances(name, description, essentialoils, topnotes, middlenotes, basenotes, reference, year, type, price, fragrancehouse, triangle, extractname, extractreference, extractprice, extractfragrancehouse, disccolour, collarcolour, actuatorcolour)
    VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
mysqli_stmt_bind_param($stmt, 'sssssssssssssssssss', $name, $description, $essentialoils, $topnotes, $middlenotes, $basenotes, $reference, $year, $type, $price, $fragrancehouse, $triangle, $extractname, $extractreference, $extractprice, $extractfragrancehouse, $disccolour, $collarcolour, $actuatorcolour);

mysqli_stmt_execute($stmt); 

我在这里查看了许多不同的问题,但他们的解决方案似乎都不适用于我的问题,有人知道问题是什么吗?

【问题讨论】:

标签: php mysql mysqli bindparam


【解决方案1】:

您的INSERT 语句无效:VALUES 子句必须在括号中带有?(并且在括号中的字段名称之后)。另外一个好的做法是在分配后检查$stmt

$stmt = mysqli_prepare($db, 
   "INSERT INTO fragrances (name, description, essentialoils, topnotes, middlenotes, basenotes, reference, year, type, price, fragrancehouse, triangle, extractname, extractreference, extractprice, extractfragrancehouse, disccolour, collarcolour, actuatorcolour)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if ($stmt) {
    mysqli_stmt_bind_param($stmt, 'sssssssssssssssssss', $name, $description, $essentialoils, $topnotes, $middlenotes, $basenotes, $reference, $year, $type, $price, $fragrancehouse, $triangle, $extractname, $extractreference, $extractprice, $extractfragrancehouse, $disccolour, $collarcolour, $actuatorcolour);

    mysqli_stmt_execute($stmt);
    // ...
} else
    printf("Error: %s\n", mysqli_error($db));

【讨论】:

  • 不幸的是,这仍然会产生相同的错误消息(删除 if 语句)
【解决方案2】:

$stmt 只有在 mysqli_prepare 返回 false 时才会变为布尔值。

当这种情况发生时,这意味着它未能准备查询,因此您需要检查错误:

$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, 'INSERT INTO fragrances VALUES...')) {
    //it's all good bind and execute here
}else{
   //we have a problem
   printf("Errormessage: %s\n", mysqli_error($db));
}

【讨论】:

  • 原来我混淆了我的美国主义,并且错误地有一个名为 colcolor 的字段!这导致我出错,所以得到了勾号,谢谢
【解决方案3】:

这意味着您的 SQL 无效,因为 prepare 返回 false;

你的 SQL 应该是;

$stmt = mysqli_prepare($db, "INSERT INTO fragrances VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");

每一个?是分别表示每个参数需要绑定的地方。

【讨论】:

    【解决方案4】:

    错误消息意味着您的 mysqli_prepare 返回了一个布尔值(对于您的情况,它返回了 false)。

    您需要将所有字段名称替换为字符? 以进行准备好的声明。这就是它的工作原理。

    See example in the official documentation

    EDIT See also mysqli_error ,它将详细说明您的错误。事实上,在使用变量之前,您应该始终检查它:

    $stmt = mysqli_prepare($db, "....");
    if(!$stmt)
      echo mysqli_error($db); // display error only for debug. Avoid this in production
    

    【讨论】:

    • 不幸的是,这并没有改变我的错误信息
    • 所以你还有一个错误。可能与$db 有关。我编辑了答案以帮助您调试它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2015-07-05
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    相关资源
    最近更新 更多