Tôi biết câu hỏi này là cổ, nhưng đối với những người vẫn làm việc với 5.2.4 và nhận được lỗi này, bạn có thể xem xét việc tạo một đối tượng PDS mysql mới để giải quyết vấn đề này.
Tôi vẫn sử dụng 5.2.4 trên máy chủ dev của mình để đảm bảo khả năng tương thích ngược cho các plugin WordPress mà tôi phát triển. Dưới đây là một trình bao bọc xung quanh các cuộc gọi thủ tục mà tôi sử dụng để gọi thành công các thủ tục trong cả 5.2.4 (chạy trên máy chủ dev của tôi), thường sẽ cho tôi lỗi và máy chủ sản xuất của tôi (chạy phiên bản mới hơn) không đưa ra lỗi).
WordPress cụ thể của nó, nhưng sẽ không khó để sửa đổi nó bằng cách sử dụng php thẳng.
/*
* Need to cache connection so we don't keep creating connections till we hit max.
*/
private $_dbhCache=null;
/**
* mySQL Call Proc
*
* Provides a wrapper around calling a mySQL stored procedure to ensure against a 5.2.4 bug that
* causes procedure calls to fail.
* Error:'can't return a result set in the given context'
*
* references:
* http://stackoverflow.com/questions/1200193/cant-return-a-result-set-in-the-given-context
* http://php.net/pdo_mysql#69592 //i got empty result set but pointed me in the right direction
* http://php.net/pdo_mysql#80306 //this worked, but returned 0-indexed and assoc, i edited it so it only returns assoc mimicking $wpdb->get_results(
* http://www.php.net/manual/en/pdo.connections.php
* http://www.php.net/manual/en/pdostatement.fetch.php explains about FETCH_ASSOC
*
* @param string $proc The mySQL stored procedure string, including paramaters, but without the call statement. e.g.: "my_procedure_name('my_paramater')";
* @return string The results of the procedure call
*/
public function mySQLCallProc($proc) {
global $wpdb;
$query = "call $proc";
try {
/*
* Attempt to call the procedure normally.
*
*/
$query_result = $wpdb->get_results($query, ARRAY_A);
/*
* Check for a database error
* and throw an exception if one is found.
* We can then attempt it again using a workaround.
*/
if ($wpdb->last_error !== '') {
throw new Exception('Database Error While Calling Procedure');
}
} catch (Exception $e) {
try {
/*
* Create a PDO Object for the connection
*/
if (is_null($this->_dbhCache)) {
$dbh = new PDO('mysql:host=' . DB_HOST . ';port=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true));
$this->_dbhCache=$dbh ;
}else{
$dbh = $this->_dbhCache;
}
/*
* Prepare and call the procedure.
*/
$stmt = $dbh->prepare("call $proc");
$stmt->execute();
/*
* fetch all rows into an associative array.
*/
$query_result = $stmt->fetchAll(PDO::FETCH_ASSOC); //FETCH_ASSOC gets results as an assoc array. without it, you'll receive both assoc and 0-indexed array
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
return ($query_result);
}
Lời khuyên rất tốt từ bạn, cảm ơn sự chính xác của bạn. – Farid
Sự cố này có thể trở lại trong 5.2.17 không? Tôi nhận được cùng một lỗi nhưng CentOS PHP phiên bản sau này. – Clutch
Có lẽ một số vấn đề khác khi tôi tìm thấy vấn đề tương tự trên phiên bản 5.3.1 bằng cách sử dụng xammp và windows – Sydwell