MySqli命令不同步;你现在不能运行这个命令

时间:2013-04-03 21:45:44

标签: php mysql mysqli

我已将我的注册脚本从mysql转换为mysqli。我工作得很好,但它现在给了我错误

Commands out of sync; you can't run this command now

这是我用来注册用户的功能

function register_user($register_data) { 
global $myConnection;
array_walk($register_data, 'array_sanitize'); 
//Make the array readable and seperate the fields from data 
$fields = '`' . implode('`, `', array_keys($register_data)) . '`'; 
$data = '\'' . implode('\', \'', $register_data) . '\''; 
//Insert the data and email an activation email to the user 
mysqli_query($myConnection, "INSERT INTO `members` ($fields) VALUES ($data)") or die(mysqli_error($myConnection)); 
email($register_data['mem_email'], 'Activate your account', "Hello " . $register_data['mem_first_name'] . ",\n\nThank you for creating an account with H Fencing. Please use the link below to activate your account so we can confirm you identity:\n\nhttp://blah.blah.co.uk/activate.php?mem_email=" . $register_data['mem_email'] . "&email_code=" . $register_data['email_code'] . "\n\n - David & Jay "); 
}

电子邮件可以使用我的数组中的正确数据发送。但是没有数据插入数据库,我得到上面提到的错误。我以前从未遇到过这个错误。

2 个答案:

答案 0 :(得分:9)

  

如果 命令不同步;您现在无法在客户端代码 中运行此命令,而是以错误的顺序调用客户端函数。

     

例如,如果您使用mysql_use_result()并尝试在调用mysql_free_result()之前执行新查询,则会发生这种情况。如果您尝试执行两个返回数据的查询而不在其间调用mysql_use_result()或mysql_store_result(),也会发生这种情况。

从这里: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

<强>更新

如果为查询创建变量并将变量直接粘贴到MySQL Workbench之类的东西,则可以在执行之前检查语法。

<?php
            function myConnection(){
              $myConnection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
              return $myConnection;
            }   


    function register_user($register_data) { 
        array_walk($register_data, 'array_sanitize'); 
        //Make the array readable and seperate the fields from data 
        $fields = '`' . implode('`, `', array_keys($register_data)) . '`'; 
        $data = "'" . implode("', '", $register_data) . "'"; 
        //Insert the data and email an activation email to the user 
        $query = "INSERT INTO `members` ($fields) VALUES ($data)";
                    $myNewConnection = myConnection();          

                    if($result = mysqli_query($myNewConnection, $query)){ 
        email($register_data['mem_email'], 'Activate your account', "Hello " . $register_data['mem_first_name'] . ",\n\nThank you for creating an account with H Fencing. Please use the link below to activate your account so we can confirm you identity:\n\nhttp://blah.blah.co.uk/activate.php?mem_email=" . $register_data['mem_email'] . "&email_code=" . $register_data['email_code'] . "\n\n - David & Jay "); 
        mysqli_free_result($result);
         return ("Success");
        } else {
            echo $query;
            die(mysqli_error($myNewConnection));
        } 

    }

?>  

答案 1 :(得分:-1)

当我尝试在非常快的服务器上执行两个快速 SQL 命令时看到错误“命令不同步;您现在无法运行此命令”时,此答案本身不涉及 mysqli,但对我有用通过python。

cmd = "SET autocommit=0;"
cmd += "START TRANSACTION;"
cmd += "sql cmd #1;"
cmd += "sql cmd #2;"
cmd += "COMMIT;"