Can't return a result set in the given context

前端 未结 5 2160
执笔经年
执笔经年 2020-11-28 16:23

When ever I try to call store procedure in mysql that sends back a result set, it keeps saying me that \"can\'t return a result set in the given context\".

I\'ve goo

5条回答
  •  没有蜡笔的小新
    2020-11-28 16:54

    I know this question is ancient, but for those still working with 5.2.4 and getting this error, you may consider creating a new mysql PDO object to work around this problem.

    I still use 5.2.4 on my dev server to ensure backward compatibility for the WordPress plugins I develop.

    Below is a wrapper around procedural calls that I use to successfully call procedures in both 5.2.4 (run on my dev server) , which would normally give me the error, and my production server (which runs a newer version that doesn't give the error) .

    Its WordPress specific, but it wouldn't be difficult to modify it using straight 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() . "
    "; die(); } } return ($query_result); }

提交回复
热议问题