working with null in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

working with null in php

Post by merofelde »

I have three fields in a table.
the last field does not always have data in it.
I want to display only the rows where the third field is empty ( or NULL )

Code: Select all

//connection strings
$result = mysql_query("SELECT field1, field2, field3 FROM tablename WHERE field3 IS NULL");
while($row = mysql_fetch_array($result))
{
echo $row['field1'] . $row['field2'];
echo "<hr></hr>";
}
//close connection
I am trying to generate a list of those records that are missing data from field3.
Any thoughts?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: working with null in php

Post by Celauran »

Is the field allowed to be NULL? You could try WHERE field3 = ''
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Re: working with null in php

Post by merofelde »

Celauran wrote:Is the field allowed to be NULL? You could try WHERE field3 = ''
the field does not have to contain data. It is an optional field.
So I could try WHERE field3 = NULL ???
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: working with null in php

Post by Celauran »

See if the schema allows NULL or not. Empty and NULL aren't the same. I'd still check for WHERE field3 = ''
merofelde
Forum Newbie
Posts: 9
Joined: Wed Oct 26, 2011 1:12 pm

Re: working with null in php

Post by merofelde »

Celauran wrote:See if the schema allows NULL or not. Empty and NULL aren't the same. I'd still check for WHERE field3 = ''
went to phpmyadmin and put a check in the box next to Null.
and changed the WHERE field3 = ' '" and it worked.
Thanks :-)
Post Reply