【问题标题】:PHP: Commands out of sync; you can't run this command now [duplicate]PHP:命令不同步;您现在无法运行此命令 [重复]
【发布时间】:2019-07-04 06:41:21
【问题描述】:

我通常从数据库中读取数据,但使用while时出现错误。

我的代码是:

$BarberId    = 1;
$stmt = $db->prepare("CALL `GetBranch`(?);");
$stmt->bind_param('i', $BarberId);
$stmt->execute();

$Tree_Barber_Id = NULL;
$stmt->bind_result($Tree_Barber_Id);
$stmt->store_result();

if($stmt->num_rows)
{
    while($stmt->fetch())
    {        
        $Priod = NULL;
        $stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");

        $stmt2->bind_param('i', $Tree_Barber_Id); //ERROR IS HERE!!!

        $stmt2->execute();
        $stmt2->bind_result($Priod);
        $stmt2->store_result();
    }
}

$stmt->close();

我认为这个错误是因为变量stmt 还没有关闭。但是通过 $stmt->close(); while 命令关闭它将不起作用。

错误是:

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17

【问题讨论】:

标签: php database mysqli while-loop


【解决方案1】:

阅读错误信息。

致命错误:在第 17 行的 C:\xampp\htdocs\file1.php 中的布尔值上调用成员函数 bind_param()

第 17 行是这样的:

$stmt2->bind_param('i', $Tree_Barber_Id);

错误消息告诉您$stmt2 是一个布尔值。 $stmt2 来自这一行:

$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");

mysqli的prepare()函数returns false如果有错误。

这意味着您的查询无效。你可以通过查看$db->error来找出它的无效之处

【讨论】:

  • 命令直接在MySql中执行。 $db->e​​rror 是“命令不同步;你现在不能运行这个命令”
  • 好的,也许this question 中的答案可以为您指明解决问题的正确方向
  • 其实我在使用存储过程命令后还要使用如下命令:$db->next_result();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 2013-12-27
  • 2012-12-01
相关资源
最近更新 更多