如何检查 mysql 条目在 PhP 中是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1591611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if mysql entry is empty in PhP?
提问by David Willis
here is the description variable I am echoing from my table:
这是我从表中回显的描述变量:
$description = mysql_result($result,$i,"description");
sometimes the $i'th record is empty and doesn't have any data in it/no description.
有时第 $i 条记录是空的,其中没有任何数据/没有描述。
What I want to do is echo "No description available" for records that are empty
我想要做的是为空记录回显“无可用描述”
if (isset($description)){ echo "No description available";}
else{ echo $desctipion;}
My attempt doesn't work though as it then echoes No description available for every record even those which aren't empty.
我的尝试不起作用,因为它随后回显 No description available for each record 即使那些不是空的。
What is the answer?
答案是什么?
回答by Matt Huggins
回答by intuited
It depends on what you mean.
这取决于你的意思。
mysql_resultreturns FALSEon failure, which would happen if you specified an invalid (row,field). To check for this you'd want to do use the identicality operator ===, which checks both value and type. The equality operator ==, the empty()function, and the conditional evaluation of an if statement all check the value but not the type.
mysql_resultFALSE在失败时返回,如果您指定了无效的 (row,field),就会发生这种情况。要检查这一点,您需要使用相同运算符===,它检查值和类型。相等运算符==、empty()函数和 if 语句的条件评估都检查值而不是类型。
This means that using one of those methods there's no difference between various values that all equate to Boolean FALSE, like empty strings, empty arrays, the string '0', and the value NULL.
这意味着使用这些方法之一,所有等同于 的各种值之间没有区别Boolean FALSE,例如空字符串、空数组、字符串'0'和值NULL。
So if you want to be really thorough about it you could do something like the following:
因此,如果您想真正彻底了解它,您可以执行以下操作:
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;
}
Since empty()returns trueunder a similar set of conditions to an equality (==) with FALSE, this more strict in your type checking would be especially important in cases where the result might actually be "0".
由于在一组类似的条件下empty()返回true与等于 ( ==) FALSE,因此在您的类型检查中更严格的在结果可能实际为 的情况下尤其重要"0"。
Apparently I'm not allowed to post more than one hyperlink, so I wasn't able to link to the documentation for comparison operators ("http://php.net/manual/en/language.operators.comparison.php") or the empty function ("http://php.net/empty"). Fortunately their security is relatively lax. Mwuh Hah!
显然我不允许发布多个超链接,所以我无法链接到比较运算符的文档(“ http://php.net/manual/en/language.operators.comparison.php”)或空函数(“ http://php.net/empty”)。幸运的是,他们的安全性相对宽松。嗯哼!
回答by Cesar
Use the mysql_affected_rowsfunction:
使用mysql_affected_rows函数:
$qtd = mysql_affected_rows($result);

