簡體   English   中英

MySQL查詢通過工作台工作,但不通過mysqli工作

[英]MySQL query works through workbench but not 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,現在一切正常。

查看$con轉儲后,似乎您在此連接上執行的最后一個查詢未使用其結果。 mysqli默認情況下使用無緩沖查詢,因此您需要使用store_result()free_result()手動執行的上一個查詢的結果手動提取到緩沖區中,或者釋放用於新查詢的空間以store_result() free_result() 。分別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM