Checking for an empty field with MySQL

PhpSqlMysqlNull

Php Problem Overview


I've wrote a query to check for users with certain criteria, one being they have an email address.

Our site will allow a user to have or not have an email address.

$aUsers=$this->readToArray('
 SELECT `userID` 
 FROM `users` 
 WHERE `userID` 
 IN(SELECT `userID`
         FROM `users_indvSettings`
  WHERE `indvSettingID`=5 AND `optionID`='.$time.')
  AND `email`!=""
 ');

Is this the best way to check for an empty field in SQL? I've just tried "IS NOT NULL" and this still returned a users record without them having an email address.

The query above works but out of curiosity I wondered if I'm doing it the correct way.

Php Solutions


Solution 1 - Php

An empty field can be either an empty string or a NULL.

To handle both, use:

email > ''

which can benefit from the range access if you have lots of empty email record (both types) in your table.

Solution 2 - Php

Yes, what you are doing is correct. You are checking to make sure the email field is not an empty string. NULL means the data is missing. An empty string "" is a blank string with the length of 0.

You can add the null check also

AND (email != "" OR email IS NOT NULL)

Solution 3 - Php

You could use

IFNULL(email, '') > ''

Solution 4 - Php

There's a difference between an empty string (email != "") and NULL. NULL is null and an Empty string is something.

Solution 5 - Php

This will work but there is still the possibility of a null record being returned. Though you may be setting the email address to a string of length zero when you insert the record, you may still want to handle the case of a NULL email address getting into the system somehow.

     $aUsers=$this->readToArray('
     SELECT `userID` 
     FROM `users` 
     WHERE `userID` 
     IN(SELECT `userID`
               FROM `users_indvSettings`
               WHERE `indvSettingID`=5 AND `optionID`='.$time.')
     AND `email` != "" AND `email` IS NOT NULL
     ');

Solution 6 - Php

If you want to find all records that are not NULL, and either empty or have any number of spaces, this will work:

LIKE '%\ '

Make sure that there's a space after the backslash. More info here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

Solution 7 - Php

check this code for the problem:

$sql = "SELECT * FROM tablename WHERE condition";

$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res)) {

    foreach($row as $key => $field) {  
      
        echo "<br>";

        if(empty($row[$key])){

 			echo $key." : empty field :"."<br>"; 
      	
        }else{

        echo $key." =" . $field."<br>"; 	

        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser275074View Question on Stackoverflow
Solution 1 - PhpQuassnoiView Answer on Stackoverflow
Solution 2 - PhpYadaView Answer on Stackoverflow
Solution 3 - PhpMathieu de LorimierView Answer on Stackoverflow
Solution 4 - PhpLeslieView Answer on Stackoverflow
Solution 5 - PhppdavisView Answer on Stackoverflow
Solution 6 - PhpScott HildebrandView Answer on Stackoverflow
Solution 7 - Phprahulcs754View Answer on Stackoverflow