处理空值和 JSON


Dealing with null value and JSON

我正在尝试使用IFNULL()函数来防止查询结果为空, 因为当结果转换为 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

我想你有一个错字。 状态前缺少 ' 。

   IFNULL(status,'nothing')