![](/img/trans.png)
[英]PHP Array from MySQL is not empty but has an empty entry - how to avoid
[英]How to check if mysql entry is empty in PhP?
這是我從表中回應的描述變量:
$description = mysql_result($result,$i,"description");
有時$ i'th記錄是空的,沒有任何數據/沒有描述。
我想要做的是回復“沒有描述可用”的記錄是空的
if (isset($description)){ echo "No description available";}
else{ echo $desctipion;}
我的嘗試不起作用,因為它然后回應沒有描述可用於每個記錄,即使那些非空。
答案是什么?
isset($description)
將為true,因為$ description仍然設置,即使其值為“empty”。 你需要使用的是空的 。
if (empty($description)) {
echo "No description available";
} else {
echo $description;
}
這取決於你的意思。
mysql_result在失敗時返回FALSE
,如果指定了無效(行,字段),則會發生這種情況。 要檢查這個,你想要使用相同運算符===
,它同時檢查值和類型。 等於運算符==
, empty()
函數和if語句的條件求值都檢查值而不是類型。
這意味着使用其中一種方法,各種值之間沒有區別,所有值都等於Boolean FALSE
,如空字符串,空數組,字符串'0'
和值NULL
。
因此,如果您想要真正徹底了解它,您可以執行以下操作:
if ($description === FALSE) {
throw new Exception("The row $i was out of range in query $query.");
} else if ($description === NULL) {
// assuming that the description field has a default value of NULL
// * this one I'm not sure about.. the documentation for mysql_result claims
// that it returns a string, so this may never happen.
// It's left as an exercise for the reader.
throw new Exception("Uninitialized value in result row $i of db query $query");
} else if ($description === '') {
echo "No description available";
} else {
echo $description;
}
由於empty()
在類似的條件集下返回true
,使用FALSE
返回等式( ==
),因此在結果可能實際為"0"
情況下,類型檢查中更嚴格的特別重要。
顯然我不允許發布多個超鏈接,所以我無法鏈接到比較運算符的文檔(“ http://php.net/manual/en/language.operators.comparison.php ”)或空函數(“ http://php.net/empty ”)。 幸運的是,他們的安全性相對寬松。 Mwuh Hah!
使用mysql_affected_rows
函數:
$qtd = mysql_affected_rows($result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.