【问题标题】:Can't return a result set in the given context无法在给定上下文中返回结果集
【发布时间】:2010-11-15 01:43:12
【问题描述】:

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

我google了一下,有人说是mysql的bug,有人说你应该改变你的mysqli驱动......

情况:

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

我该怎么办!?

【问题讨论】:

    标签: php zend-framework mysqli multi-select


    【解决方案1】:

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

    实际上,经过快速搜索,您所看到的问题似乎是在 PHP 5.2.3 和 PHP 5.2.4 之间引入的(并且在 PHP 5.2.5 中仍然存在)。
    bug #42548 : PROCEDURE xxx can't return a result set in the given context (works in 5.2.3!!)

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


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


    当您使用 Zend Framework 1.9 时,这里有另一篇您可能感兴趣并且可能更容易测试的帖子:stored procedure error after upgrade to 1.8

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


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

    【讨论】:

    • 这个问题会在 5.2.17 中再次出现吗?我得到了同样的错误,但 CentOS PHP 更高版本。
    • 可能是其他一些问题,因为我在使用 xammp 和 windows 的 5.3.1 版本上发现了类似的问题
    【解决方案2】:

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

    【讨论】:

    • 感谢您提供您选择的解决方案 :-)(请注意:使用 PHP 5.3 可能会导致代码中其他地方出现其他问题,因为它会带来很多新东西^^)
    【解决方案3】:

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

    【讨论】:

    • 在尝试部署在 PHP 5.3 开发服务器上开发的系统时,我很困惑地在 PHP 5.2.6 上遇到了这个问题。所以我必须升级。注意有backward incompability issues
    • @Gruber, ?总是存在向后不兼容的问题。这些版本有什么特别之处?
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 2013-03-21
        • 2016-01-13
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多