如何检查MySQL是否返回null/empty? [英] How to check if MySQL returns null/empty?

查看:217
本文介绍了如何检查MySQL是否返回null/empty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 单独使用空就可以实现当前的用法,如果要区分空字段和空字段,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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆