MySQL查询通过工作台而不是mysqli工作

时间:2016-04-23 22:57:28

标签: php mysql mysqli

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

公共职能:

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,现在一切正常。

1 个答案:

答案 0 :(得分:1)

查看$con转储后,您在此连接上执行的最后一个查询似乎未使用其结果。默认情况下,mysqli使用无缓冲的查询,因此您需要手动获取先前执行的查询的结果,或者释放它用于为新查询腾出空间的内存,{{1}或分别为store_result()