2012-05-01 50 views
2

我試圖使用IFNULL()函數來防止查詢結果爲空,因爲當結果轉換爲JSON數組時,空值會導致錯誤。處理空值和JSON

$sql = mysql_query("select IFNULL(status,'nothing'), 
    foodname from disease_food, 
    food where disease_food.Disease_ID=$d1 or disease_food.Disease_ID=$d2 and  
    Food_ID=$res1 and disease_food.Food_ID=food.ID"); 

while($row=mysql_fetch_assoc($sql)) { 
    $output[] = $row; 
} 

$data = json_encode($output); 
print($data); 
mysql_close(); 

的錯誤:(當結果爲空)

Undefined variable: output

+3

您必須使用準備好的語句,這些天,這種查詢是危險的,過時的:http://php.net/manual/en/pdo.prepared-statements.php – freshnode

+0

嘗試'$ SQL =請求mysql_query( 「選擇IFNULL(status,'nothing'),來自disease_food的foodname,食物,其中disease_food.Disease_ID = $ d1或disease_food.Disease_ID = $ d2和 Food_ID = $ res1 and disease_food.Food_ID = food.ID」)或者die(mysql_error ());'並告訴我什麼錯誤(如果有的話) – freshnode

+0

如何定位並修復該錯誤? –

回答

0

我想你哈瓦一個錯字。有一個'失蹤的狀態。

IFNULL(status,'nothing')