【问题标题】:MySqli Commands out of sync; you can't run this command now [duplicate]MySqli 命令不同步;您现在无法运行此命令 [重复]
【发布时间】:2013-04-03 21:45:44
【问题描述】:

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

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 "); 
}

电子邮件发送正常,包含我阵列中的正确数据。但是没有数据插入到数据库中,我得到了上面提到的错误。我以前从未遇到过这个错误。

【问题讨论】:

  • 1.在运行之前打印出查询并查看它。 2. 参数化您的查询。 3. 看在上帝的份上,参数化您的查询。

标签: php mysql mysqli


【解决方案1】:

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

这可能发生,例如,如果您使用 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));
        } 

    }

?>  

【讨论】:

  • 我将查询直接粘贴到 PHPmyadmin 中,它插入得很好,这意味着实际查询有效
  • 是的,但是它的作用是运行 else 语句并打印查询,而不是运行 if 语句
  • 运行if,问题是查询失败。您的连接语句是否按此顺序排列? $myConnection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); (当然要进行适当的设置)。
  • 是的,连接看起来相同并且工作正常,因为我将它用于网站上的功能。奇怪的是它回显了查询,但是之后没有出现错误
  • 我再次尝试的废品,查询后显示错误
猜你喜欢
  • 2019-07-04
  • 2011-06-15
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 2013-12-27
  • 2012-12-01
相关资源
最近更新 更多