MySqli命令不同步;您现在不能运行此命令 [英] MySqli Commands out of sync; you can't run this command now
问题描述
我已将注册脚本从mysql转换为mysqli.我作为mysql正常工作,但是现在却给我错误
I have converted my register script from mysql to mysqli. I worked fine as mysql however it now gives me the error
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.
推荐答案
如果您收到 命令不同步;您现在无法在客户端代码中运行此命令 ,您以错误的顺序调用了客户端函数.
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.
例如,如果您使用的是mysql_use_result()并尝试执行新的查询,然后再调用mysql_free_result(),则会发生这种情况.如果您尝试执行两个返回数据的查询而不在两者之间调用mysql_use_result()或mysql_store_result(),也会发生这种情况.
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.
从这里: http://dev.mysql.com/doc/refman/5.0/zh/commands-out-of-sync.html
更新
如果为查询设置变量并将其直接粘贴到MySQL Workbench之类的文件中,则可以在执行之前检查语法.
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.
<?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));
}
}
?>
这篇关于MySqli命令不同步;您现在不能运行此命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!