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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15798762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:58:49  来源:igfitidea点击:

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

phpmysqlmysqli

提问by jhetheringt7

I have converted my register script from mysql to mysqli. I worked fine as mysql however it now gives me the error

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

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

This is the function I use to register the user

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

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

The email sends fine with the correct data from my array. However no data is inserted into the database and I get the error mentioned above. I have never come across this error before.

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

回答by Absolute?ER?

If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

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

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

From here: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

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

Update

更新

If you make the a variable for the query and paste the variable directly into something like MySQL Workbench you can check the syntax prior to execution.

如果您为查询创建一个变量并将该变量直接粘贴到 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));
        } 

    }

?>