2013-04-03 142 views
8

我已將我的註冊腳本從mysql轉換爲mysqli。我工作得很好像MySQL但現在給我的錯誤MySqli命令不同步;您現在無法運行此命令

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

電子郵件發送罰款從我的數組的正確數據的功能。但是,沒有數據被插入到數據庫中,並且出現上述錯誤。我從來沒有遇到過這個錯誤。

+3

1.打印出查詢,然後再運行它並查看它。 2.參數化您的查詢。 3.爲了愛上帝,請參數化您的查詢。 – Sammitch 2013-04-03 21:54:52

回答

9

如果得到命令不同步;您現在無法在您的客戶端代碼中運行此命令,您按錯誤順序調用客戶端功能。

例如,如果您正在使用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)); 
     } 

    } 

?> 
+0

我將查詢直接粘貼到PHPmyadmin中,並且它插入正常,這意味着實際查詢的工作原理 – jhetheringt7 2013-04-03 22:30:47

+0

您是否使用mysqli_free_result部分嘗試此代碼? – 2013-04-03 23:30:30

+0

是的,但是它所做的是運行else語句並打印查詢而不是運行if語句 – jhetheringt7 2013-04-03 23:39:30

相關問題