php - MySQL 查询可以通过工作台工作,但不能通过 mysqli

标签 php mysql mysqli

我正在尝试调用我的 PHP 类并运行函数 get_group_count,该函数计算特定用户 ID 的组数。 mysqli 查询使用可在 MySQL Workbench 中运行但不适用于 PHP 的 mysqli 的存储过程。

公共(public)功能:

public function get_group_count($userID) {
    if($this->status()) {
        $db = Database::getInstance();
        $con = $db->getConnection();

        $pull = $con->prepare("CALL get_group_count(?)");
        $pull->bind_param('i', $userID); // line 19

        if(!$pull->execute()) {
            $pull->close();
            return false;
        };

        $pull->store_result();
        $pull->bind_result($count);
        $pull->fetch();
        $pull->close();

        return $count;
    };
    return false;
}

错误:

Call to a member function bind_param() on a non-object in ... on line 19

存储过程:

DELIMITER //
CREATE PROCEDURE get_group_count(IN userID int)
BEGIN
    SELECT (SELECT count(*) FROM groups WHERE adminID = userID) + (SELECT count(*) FROM members WHERE userID = userID) AS count;
END //
DELIMITER ;

通过 MySQL Workbench 调用该过程时,它会返回 groups 表和 members 表中的总行数。

注意:当尝试通过 mysqli 而不是存储过程运行查询时,我也遇到了相同的错误。

我该如何解决这个问题?

编辑:

$con var_dump:

object(mysqli)#4 (19) {
  ["affected_rows"]=>
  int(1)
  ["client_info"]=>
  string(6) "5.5.45"
  ["client_version"]=>
  int(50545)
  ["connect_errno"]=>
  int(0)
  ["connect_error"]=>
  NULL
  ["errno"]=>
  int(2014)
  ["error"]=>
  string(52) "Commands out of sync; you can't run this command now"
  ["error_list"]=>
  array(1) {
    [0]=>
    array(3) {
      ["errno"]=>
      int(2014)
      ["sqlstate"]=>
      string(5) "HY000"
      ["error"]=>
      string(52) "Commands out of sync; you can't run this command now"
    }
  }
  ["field_count"]=>
  int(1)
  ["host_info"]=>
  string(25) "Localhost via UNIX socket"
  ["info"]=>
  NULL
  ["insert_id"]=>
  int(0)
  ["server_info"]=>
  string(14) "5.5.45-cll-lve"
  ["server_version"]=>
  int(50545)
  ["stat"]=>
  string(52) "Commands out of sync; you can't run this command now"
  ["sqlstate"]=>
  string(5) "HY000"
  ["protocol_version"]=>
  int(10)
  ["thread_id"]=>
  int(36623197)
  ["warning_count"]=>
  int(0)
}

$pull var_dump:

bool(false)
NULL

EDIT2:最终切换到 PDO,现在一切正常。

最佳答案

查看 $con 后dump,看来您在此连接上执行的最后一个查询未使用其结果。 mysqli默认情况下使用无缓冲查询,因此您需要手动将上一个查询执行的结果获取到缓冲区中,或者释放它正在使用的内存,为新查询腾出空间,使用 store_result()free_result() ,分别。

关于php - MySQL 查询可以通过工作台工作,但不能通过 mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817389/

相关文章:

mysql - 优化MySQL单表SQL查询

PHP: "Value Expected To Be A Reference"与 Call_User_Func 和绑定(bind)参数

php - Mysql 查询连接条件

php - 制作标签云 - 没有有效的标签来源

php - 新的 Instagram API - 您如何请求标记的媒体?

php - 全局更改日期格式

php - 使用 sha1 和 salt 存储密码

php - 如何洗牌数据库值并只显示 4

php - Symfony 2.5 - 数据转换器和 View 转换器

mysql varchar字节长度255问题