8

每当我尝试在 mysql 中调用发送回结果集的存储过程时,它总是告诉我“无法在给定的上下文中返回结果集”。

我在谷歌上搜索过,有人说这是 mysql 错误,有人说你应该改变你的 mysqli 驱动程序和....

情况 :

使用 mysqli 驱动程序客户端 API 库版本 5.0.51a,PHP 版本 5.2.4-2ubuntu5.6,使用 Zend 1.9 RC 1 Mysqli 适配器。

我该怎么办!?

4

5 回答 5

5

答案是升级你的php,我刚刚升级到5.3.0,它就像Candy一样工作!

于 2009-07-29T22:31:24.807 回答
1

不确定这是否是您问题的解决方案,但是尝试使用更新版本的 PHP 怎么样?
PHP 5.2.4 确实很老了——所以,如果它是 PHP 的 mysqli 驱动程序中的一个错误,它可能已经被纠正了......

实际上,在快速搜索之后,您所看到的问题似乎是在 PHP 5.2.3 和 PHP 5.2.4 之间引入的(并且在 PHP 5.2.5 中仍然存在)。
请参阅错误 #42548:PROCEDURE xxx 无法在给定的上下文中返回结果集(适用于 5.2.3 !!)

你能用 PHP 5.2.9 或 5.2.10 之类的东西进行测试吗?
我知道这些不是 Ubuntu 提供的,即使在最后一个 Ubuntu 稳定版本中也是如此 :-( 您可能必须从源代码编译 :-(


另一个想法是尝试 mith PDO_MySql 适配器:也许它可以与那个适配器一起使用?
是否可以更改适配器而不会造成太多麻烦/无需花费数小时进行测试?


当您使用 Zend Framework 1.9 时,您可能会感兴趣,并且可能更容易测试另一篇文章:升级到 1.8 后的存储过程错误

一个简单的尝试解决方案是回到 Zend Framework 1.7;你有可能,只是为了测试?


无论如何......祝你好运!
而且,如果您找到了解决方案,请不要忘记指出问题所在,以及您是如何解决的 ;-)

于 2009-07-29T16:59:07.430 回答
1

我最近在合同上遇到了这个问题。客户在windoze和php 5.2.6上使用代码库,我的安装是linux和php 5.3.1无论我们做什么,他们都不合作,所以最后他们给了我一台windoze vista机器,我们安装了php 5.2 .6 然后我们就走了。故事的寓意:版本匹配很重要。奇怪的是,我以前在任何其他工作中都没有遇到过这种情况。但是,嘿,你不可能什么都知道。绝对不是MySql问题,只是PHP。

于 2010-02-26T22:32:08.700 回答
1

它也可以与 PHP 5.2.10 完美配合。

从早期版本开始,我已经成功地使用 mysqli::multi_query 来调用有问题的过程并获得正确的结果。

于 2010-07-02T12:52:30.907 回答
0

我知道这个问题很古老,但是对于那些仍在使用 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);


    }
于 2014-02-13T14:34:03.050 回答