Stored procedure causes “Commands out of sync” on the next query

前端 未结 5 1557
不知归路
不知归路 2020-12-16 04:58

I am running a query with a mysql stored procedure :

$AddProf_qr = mysql_query(\"call AddStudent(\'$d_Pass\', \'$d_Titl\', \'$d_Firs\', \'$d_Midd\',  \'$d_La         


        
相关标签:
5条回答
  • 2020-12-16 05:27

    mysql_free_result(client->res);

    while (mysql_more_results(client->conn))
    {
        mysql_next_result(client->conn);
    }
    

    This did the charm for me :)

    0 讨论(0)
  • 2020-12-16 05:33

    Your stored procedure is returning multiple resultsets. See this post

    Solution?

    • Use mysqli_multi_query
    • Stop using the ancient mysql library - the i in mysqli stands for "Improved" - with good reason.
    0 讨论(0)
  • 2020-12-16 05:41

    Check out here: http://us3.php.net/manual/en/function.mysql-query.php In comments, one guy claims that he made it work by setting connection flag to MYSQL_MULTI_RESULTS (131072).

    But it would be much better to use mysqli...

    0 讨论(0)
  • 2020-12-16 05:43

    @DMin Yes that's would work, but you'll crash the server sooner or later. Just make the math, one resquest to a page that makes 3 * number of procedures to database! Just think about it!

    [UPDATE] solution:

    $aCategory = array();
    $it=0;
    $res = $mysqli->multi_query( "call ListCategory();" );
    if( $res ) {
      do {
        if ($result = $mysqli->store_result()) { 
    
            while( $row = $result->fetch_row() ) {
                    $aCategory[$it] =$row;
                    $it= $it + 1;
            }
            $result->close();
        }
      } while( $mysqli->next_result() );
    }
    
    foreach($aCategory as $row){
        echo . $row[0] . " - " . $row[1] . "<br />";
    } 
    

    Just wanted to add that you are ready to call the next Routine.

    PS: By this way I couldn't use

    echo $aCategory['category_id'] ; 
    //or 
    echo $aCategory->category_id;
    //just
    echo $aCategory[0] 
    
    0 讨论(0)
  • 2020-12-16 05:48

    Result sets returned from a stored procedure cannot be fetched correctly using mysqli_query(). The mysqli_query() function combines statement execution and fetching the first result set into a buffered result set, if any. However, there are additional stored procedure result sets hidden from the user which cause mysqli_query() to fail returning the user expected result sets.

    Result sets returned from a stored procedure are fetched using mysqli_real_query() or mysqli_multi_query(). Both functions allow fetching any number of result sets returned by a statement, such as CALL.

    look at official manual

    0 讨论(0)
提交回复
热议问题