How to check if MySQL returns null/empty?

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 = "".

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:

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use empty() and/or 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.

Method 2

You can use is_null() function.

http://php.net/manual/en/function.is-null.php : in the comments :

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:

[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"];
[…]

Method 3

select FOUND_ROWS();

will return no. of records selected by select query.

Method 4

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.

Method 5

if ( (strlen($ownerID) == 0) || ($ownerID == '0') || (empty($ownerID )) )

if $ownerID is NULL it will be triggered by the empty() test

https://www.php.net/empty

Method 6

If your datatype in your mysql database is an auto-increment primary key, then you can’t use is_null() you must use empty() or in code, it would be if (empty($id)) {

Method 7

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 "";


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x