2016-08-11 69 views
-1

我遇到了麻煩,我想檢查user_name是否爲空,那麼它會回顯YES。但是現在我可以得到任何回聲結果。我收到:如何檢查數據庫中的字段是否爲空?

錯誤:命令不同步;你現在不能運行這個命令{「success」:1}

if ($result = $mysqli->query("SELECT user_name FROM user WHERE user_id = '33'", MYSQLI_USE_RESULT)){ 

     while ($row = mysql_fetch_array($result)) { 
      if($row['user_name']===' '){ 
       echo "YES"; 
      } 
     } 

      /* Note, that we can't execute any functions which interact with the 
       server until result set was closed. All calls will return an 
       'out of sync' error */ 
      if (!$mysqli->query("SET @a:='this will not work'")) { 
       printf("Error: %s\n", $mysqli->error); 
      } 

      $result->close(); 
      $json['success'] = 1; 

}else{ 
    $json['success'] = 7; 
} 

那麼,我的問題和如何解決?我曾嘗試在網上找到答案和解決方案,但沒有一個可行。 感謝您的指導和建議。

+0

你把空間,在你,如果聲明==='' –

+1

@ Savage Leo [this](http://stackoverflow.com/questions/15798762/mysqli-commands-out-of-sync-you-cant-run-this-command-now)問題可能幫助你瞭解什麼是錯的。 – Gunaseelan

回答

0
while ($row = mysql_fetch_array($result)) { 
      if($row['user_name']===''){ 
       echo "YES"; 
      } 
     } 
+0

我得到了我提到的錯誤: 錯誤:命令不同步;你現在不能運行這個命令{「success」:1} –

0

試試這個:

if($row['user_name']===' ') to if(empty($row['user_name'])); 
0

使用空()或STRCMP(如果爲空)...否則is_null()如果爲null從PHP .net你可以看到存儲在數據庫

+0

nope。無法工作 –

0

如果使用MYSQLI_USE_RESULT,所有後續調用將返回錯誤命令不同步,除非調用mysqli_free_result()

所以,要麼你用

1)MYSQLI_STORE_RESULTmysqli_free_result()

2)mysql_query //傳統

3)再次建立連接(重新連接到DB)

相關問題