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
https://forums.phpfreaks.com/topic/65800-check-for-nulls-in-mysql_fetch_array/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.