php 如何检查 MySQL 是否返回 null/empty?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1693428/
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 returns null/empty?
提问by Steven
In DB I have a table with a field called fk_ownerID. By default, when I add a new table row, the fk_ownerIDis empty. In Toad for MySQL, this is shown as {null}. If fk_ownerIDis given a value, and I later remove this value, I set fk_ownerID = "".
在数据库中,我有一个名为fk_ownerID. 默认情况下,当我添加一个新的表格行时,它fk_ownerID是空的。在 Toad for MySQL 中,这显示为{null}. 如果fk_ownerID给出了一个值,然后我删除了这个值,我设置了fk_ownerID = "".
Now, I have the following code:
现在,我有以下代码:
$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"];
}
}
Now the variable $ownerID should have a number, or not. But I'm unsure how to check this. Currently I'm doing this:
现在变量 $ownerID 应该有一个数字,或者没有。但我不确定如何检查这个。目前我正在这样做:
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?
检查行字段是空还是空的最佳方法是什么?
回答by scragar
Use empty() and/or is_null()
使用 empty() 和/或 is_null()
http://www.php.net/emptyhttp://www.php.net/is_null
http://www.php.net/empty http://www.php.net/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.
Empty 单独将实现您当前的使用,如果您想区分空字段和空字段, is_null 只会使更多控制成为可能。
回答by Valentin Golev
You can use is_null() function.
您可以使用 is_null() 函数。
http://php.net/manual/en/function.is-null.php: in the comments :
http://php.net/manual/en/function.is-null.php:在评论中:
mdufour at gmail dot com 20-Aug-2008 04:31 Testing for a NULL field/column returned by a mySQL query.
Say you want to check if field/column “foo” from a given row of the table “bar” when > returned by a mySQL query is null. You just use the “is_null()” function:
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"];
[…]
回答by Sunil Kumar
select FOUND_ROWS();
will return no. of records selected by select query.
将返回没有。选择查询选择的记录数。
回答by Stnfordly
if ( (strlen($ownerID) == 0) || ($ownerID == '0') || (empty($ownerID )) )
if $ownerIDis NULLit will be triggered by the empty()test
如果$ownerID为NULL它将由empty()测试触发
回答by Citizen
Also, don't forget the === operator when you're working with numbers that could mean null or 0 or return some form of false or null that isn't what you're looking for.
此外,当您处理可能表示 null 或 0 的数字或返回某种形式的 false 或 null 时,请不要忘记 === 运算符,这不是您要查找的内容。
回答by Omkar Frozen
Suppose
认为
$row=mysql_fetch_row($rc)
and if you want to check if row[8] is null then do
$field=$row[8];
if($field)
echo "";
else
echo "";

