【问题标题】:mysql stored procedure error Commands out of sync; you can't run this command nowmysql存储过程错误命令不同步;你现在不能运行这个命令
【发布时间】:2018-05-11 13:31:03
【问题描述】:

我有一个从 PHP 调用的存储过程,它在运行 Maria DB 的开发版本上运行良好。我转移到运行 mysql (5.5.53) 的生产环境中,“命令不同步;你现在不能运行这个命令'错误。我已尝试建议以确保在每次查询后关闭连接。我也试过 $stmt->store_result(); & $stmt->free_result();。如果我尝试 $stmt-next_result();我没有得到这个不同步错误,但我的脚本存在循环并且我没有得到任何其他结果。

存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test3`(IN `Input_Value` INT, OUT `Out_val` VARCHAR(150))
BEGIN
    SET @sql = 'SELECT sample_name FROM sample';
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
END

PHP: `

foreach($locations as $location_name){
    echo $location_name.'<br>';
    $sp_query = "CALL test3(?,@Out_val)";
    $stmt = $dbc->prepare($sp_query);
    if(!$stmt){
        echo mysqli_error($dbc);
    }
    $stmt->bind_param('i',$view);

    if ($stmt->execute()){
        $params = array();
        $meta = $stmt->result_metadata();
        print_r($meta); 

        while ($field = $meta->fetch_field()){
            $params[] = &$row[$field->name]; 
        }
        print_r($params); 
        call_user_func_array(array($stmt, 'bind_result'), $params); 
        $header_ct = 0; 

        $labels = '';
        while ($stmt->fetch()) {
            foreach($row as $key => $value){
                $key = htmlspecialchars($key);
                //echo "Key:".$key."<br>";
                //echo "Value:".$value."<br>";
            }
        }
    }else{
        echo mysqli_error($dbc);
        echo "not working";
    }
    $stmt->store_result();
    //$stmt->free_result();
    $stmt->close();                 
}           

?>`

【问题讨论】:

    标签: php mysql stored-procedures


    【解决方案1】:

    只需将您的查询与$select_stmt-&gt;close(); 分开即可拆分它们(不是同时的,而是程序的。 For more on this see

    如果您使用的是 codeigniter 框架, 将以下代码添加到 /system/database/drivers/mysqli/mysqli_result.php For codeigniter

    function next_result()
     {
         if (is_object($this->conn_id))
         {
             return mysqli_next_result($this->conn_id);
         }
    }
    

    然后在调用存储过程时在模型中

    $query    = $this->db->query("CALL test()");
    $res      = $query->result();
    
    //add this two line 
    $query->next_result(); 
    $query->free_result(); 
    //end of new code
    
    return $res;
    

    【讨论】:

    • 我有 $stmt-close();在我的 php 代码中。你是说我放错地方了吗?
    猜你喜欢
    • 2013-04-08
    • 2021-06-11
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2018-05-24
    • 2016-05-28
    • 2015-04-16
    相关资源
    最近更新 更多