Jump to content

check for nulls in mysql_fetch_array


clinto

Recommended Posts

Hi All

 

I am trying to retreive data from the mysql db and then show the data in a html table structure. There are nulls in some db fields so the html cell doesnt draw a border around the data with nulls.

 

I am trying to check for nulls in the code and if a row field value is NULL then give it a value of "none". The if statement is not working the way i had hoped. Heres my code:

 

<?php

 

while ($row=mysql_fetch_array($mysql_result))

{

$title=$row["Title"];

 

if (is_null($row["StatusComment"])) {

$statuscomment="none" ;

}else {

$statuscomment=$row["StatusComment"];

}

 

$datelisted=$row["DateListed"];

 

 

echo "<TR><TD>$title</TD<TD>$statuscomment</TD><TD>$datelisted</TD</TR>";

}

}

mysql_close($connection);

?>

 

Thanks in advance.

Link to comment
Share on other sites

In your sql statement you can use IFNULL function like this:

 

SELECT IFNULL( StatusComment, 'NONE' ) FROM `mytable`

 

so in the result set if the StatusComment is null somewhere it will be replaced with 'NONE' and you don't have to check anything in the PHP code

Link to comment
Share on other sites

When I run the query in PhpMyAdmin the recordset still contains some records with data and some records that are empty. So I'm guessing that the empty records aren't NULL else the select statement would have changed them to 'NONE'

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.