So che questa domanda è antica, ma per coloro che stanno ancora lavorando con 5.2.4 e ricevendo questo errore, si può prendere in considerazione la creazione di un nuovo oggetto PDO mysql per ovviare a questo problema.
Uso ancora il 5.2.4 sul mio server di sviluppo per garantire la compatibilità con le versioni precedenti dei plugin WordPress che sviluppo.
Di seguito è riportato un wrapper sulle chiamate procedurali che utilizzo per chiamare correttamente le procedure in entrambi 5.2.4 (eseguito sul mio server di sviluppo), che normalmente mi restituisce l'errore e il mio server di produzione (che esegue una versione più recente che non dà l'errore).
Il suo specifico WordPress, ma non sarebbe difficile modificarlo utilizzando il php diritto.
/*
* 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);
}
Molto molto buoni consigli da parte vostra, grazie per l'accuratezza. – Farid
Potrebbe questo problema tornare in 5.2.17? Sto ottenendo lo stesso errore ma versione successiva di CentOS PHP. – Clutch
Probabilmente qualche altro problema in quanto ho riscontrato problemi simili sulla versione 5.3.1 usando xammp e windows – Sydwell