在DB中,我有一个带有字段的表fk_ownerID.默认情况下,当我添加新表行时,它fk_ownerID是空的.在Toad for MySQL中,显示为{null}.如果fk_ownerID给出了一个值,我稍后删除了这个值,我就设置了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"];
}
}
Run Code Online (Sandbox Code Playgroud)
现在变量$ ownerID应该有一个数字.但我不确定如何检查这个.目前我这样做:
if ( (strlen($ownerID) == 0) || ($ownerID == '0') || ($ownerID == 'null') )
Run Code Online (Sandbox Code Playgroud)
但我很确定这些测试中只有一个是必要的.
检查行字段是空还是空的最佳方法是什么?
scr*_*gar 42
使用empty()和/或is_null()
http://www.php.net/empty http://www.php.net/is_null
单独清空将实现您当前的使用,如果您想要区分空字段和空字段,则is_null将使更多控制成为可能.
您可以使用is_null()函数.
http://php.net/manual/en/function.is-null.php:在评论中:
mdufour at gmail dot com 20-Aug-2008 04:31测试mySQL查询返回的NULL字段/列.
假设您要检查mySQL查询返回的>"bar"的给定行中的字段/列"foo"是否为空.你只需使用"is_null()"函数:
[connect…]
$qResult=mysql_query("Select foo from bar;");
while ($qValues=mysql_fetch_assoc($qResult))
if (is_null($qValues["foo"]))
echo "No foo data!";
else
echo "Foo data=".$qValues["foo"];
[…]
Run Code Online (Sandbox Code Playgroud)