Settings | Sign in | Sign up

There are currently 3 posts.

【實驗】PHP對MySQL的NULL和空字符串的檢測

Floor 1 巨大八爪鱼 9/3/15 18:05
$sql = "SELECT NULL, ''";
$row = get_result_row($sql);
echo (int)is_null($row[0]); //1
echo (int)empty($row[0]); //1
echo (int)is_null($row[1]); //0
echo (int)empty($row[1]); //1
exit;

輸出1101
Floor 2 巨大八爪鱼 9/3/15 18:08
因此,檢測MySQL的字段值是否為NULL時,可直接使用is_null函數。
檢測是否為空字符串時,需要這樣判斷:
if (!is_null($row[1]) && empty($row[1])) {
  echo "是空字符串";
}
Floor 3 巨大八爪鱼 9/3/15 18:09
當值為NULL或空字符串時,empty都輸出1

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.