在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"];
}
}现在,变量$ownerID应该有一个数字,或者没有。但是我不知道怎么检查这个。目前我正在做这件事:
if ( (strlen($ownerID) == 0) || ($ownerID == '0') || ($ownerID == 'null') )但我非常确定这些测试中只有一项是必要的。
检查行字段为空或null的最佳方法是什么?
发布于 2009-11-07 23:38:53
使用empty()和/或is_null()
http://www.php.net/empty http://www.php.net/is_null
如果你想区分一个空字段和一个空字段,is_null将会提供更多的控制。
发布于 2009-11-07 23:38:09
您可以使用is_null()函数。
http://php.net/manual/en/function.is-null.php:在评论中:
在gmail dot com 20-Aug-200804:31测试mySQL查询返回的空字段/列。
假设您想检查表“bar”中给定行的字段/列“foo”when >是否由mySQL查询返回为null。您只需使用“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"];
[…]发布于 2015-02-16 20:31:05
select FOUND_ROWS();将返回no。select查询所选记录的个数。
https://stackoverflow.com/questions/1693428
复制相似问题