Wiem, że to pytanie jest starożytne, ale dla osób, które nadal pracują z 5.2.4 i otrzymujących ten błąd, możesz rozważyć utworzenie nowego obiektu mysql PDO, aby obejść ten problem.
Nadal używam 5.2.4 na moim serwerze, aby zapewnić zgodność wsteczną wtyczek WordPress, które rozwijam.
Poniżej znajduje się otoki proceduralne, których używam do skutecznego wywoływania procedur w wersji 5.2.4 (uruchamianej na moim serwerze dev), co zwykle daje mi błąd, i mojego serwera produkcyjnego (który uruchamia nowszą wersję nie podaje błędu).
Jego specyfikacja WordPress, ale nietrudno go zmodyfikować za pomocą prostego php.
/*
* 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);
}
Bardzo, bardzo dobre porady od was, dzięki za waszą dokładność. – Farid
Czy ten problem mógł powrócić w wersji 5.2.17? Otrzymuję ten sam błąd, ale późniejszą wersję CentOS PHP. – Clutch
Prawdopodobnie inny problem, ponieważ znalazłem podobny problem w wersji 5.3.1 z użyciem xammp i Windows – Sydwell