この質問が古いことは承知していますが、まだ 5.2.4 で作業していてこのエラーが発生する場合は、この問題を回避するために新しい mysql PDO オブジェクトを作成することを検討してください。
私は今でも開発サーバーで 5.2.4 を使用して、開発する WordPress プラグインの下位互換性を確保しています。
以下は、通常はエラーが発生する5.2.4(開発サーバーで実行)と本番サーバー(エラーを発生しない新しいバージョンを実行)の両方でプロシージャを正常に呼び出すために使用する手続き呼び出しのラッパーです。エラー) 。
WordPress 固有ですが、そのままの 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);
}