我已经转换我的注册脚本从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 ");
}
电子邮件发送罚款从我的阵列正确的数据。 但是没有数据插入到数据库中,我得到上述错误。 我从来没有碰到这个错误之前。
如果你得到命令不同步; 你不能在你的客户端代码现在运行此命令 ,您呼叫的客户端功能,以错误的顺序。
这可能发生,例如,如果你使用了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工作台,你可以先检查语法来执行。
<?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));
}
}
?>