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

前端之家收集整理的这篇文章主要介绍了php – MySqli命令不同步;你现在不能运行这个命令前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已将我的注册脚本从 mysql转换为 mysqli.我工作得很好,但它现在给了我错误
  1. Commands out of sync; you can't run this command now

这是我用来注册用户功能

  1. function register_user($register_data) {
  2. global $myConnection;
  3. array_walk($register_data,'array_sanitize');
  4. //Make the array readable and seperate the fields from data
  5. $fields = '`' . implode('`,`',array_keys($register_data)) . '`';
  6. $data = '\'' . implode('\',\'',$register_data) . '\'';
  7. //Insert the data and email an activation email to the user
  8. MysqLi_query($myConnection,"INSERT INTO `members` ($fields) VALUES ($data)") or die(MysqLi_error($myConnection));
  9. 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 ");
  10. }

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

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.

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

更新

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

  1. <?PHP
  2. function myConnection(){
  3. $myConnection = MysqLi_connect('localhost','my_user','my_password','my_db');
  4. return $myConnection;
  5. }
  6.  
  7.  
  8. function register_user($register_data) {
  9. array_walk($register_data,'array_sanitize');
  10. //Make the array readable and seperate the fields from data
  11. $fields = '`' . implode('`,array_keys($register_data)) . '`';
  12. $data = "'" . implode("','",$register_data) . "'";
  13. //Insert the data and email an activation email to the user
  14. $query = "INSERT INTO `members` ($fields) VALUES ($data)";
  15. $myNewConnection = myConnection();
  16.  
  17. if($result = MysqLi_query($myNewConnection,$query)){
  18. email($register_data['mem_email'],\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 ");
  19. MysqLi_free_result($result);
  20. return ("Success");
  21. } else {
  22. echo $query;
  23. die(MysqLi_error($myNewConnection));
  24. }
  25.  
  26. }
  27.  
  28. ?>

猜你在找的PHP相关文章