问题描述
在DB中,我有一个表,其中有一个名为fk_ownerID
的字段.默认情况下,当我添加新表行时,fk_ownerID
为空.在MySQL的Toad中,这显示为{null}
.如果为fk_ownerID
提供了一个值,然后我删除了该值,则设置为fk_ownerID = ""
.
In DB I have a table with a field called fk_ownerID
. By default, when I add a new table row, the fk_ownerID
is empty. In Toad for MySQL, this is shown as {null}
. If fk_ownerID
is given a value, and I later remove this value, I set fk_ownerID = ""
.
现在,我有以下代码:
$result = $dal->getRowByValue('tableName','id', $_POST['myID']);
// Check to see if any rows where returned
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
$ownerID = $row["fk_ownerID"];
}
}
现在变量$ ownerID应该有一个数字,否则就没有.但我不确定如何检查.目前,我正在这样做:
Now the variable $ownerID should have a number, or not. But I'm unsure how to check this. Currently I'm doing this:
if ( (strlen($ownerID) == 0) || ($ownerID == '0') || ($ownerID == 'null') )
但是我很确定这些测试中只有一项是必要的.
But I'm pretty sure only one of these tests should be necessary.
检查行字段为空还是空的最佳方法是什么?
What is the best way to check if a row field is empty or null?
推荐答案
使用empty()和/或is_null()
Use empty() and/or is_null()
http://www.php.net/empty http://www.php.net/is_null
单独使用空就可以实现当前的用法,如果要区分空字段和空字段,is_null只会使更多的控制成为可能.
Empty alone will achieve your current usage, is_null would just make more control possible if you wanted to distinguish between a field that is null and a field that is empty.
这篇关于如何检查MySQL是否返回null/empty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!