Jump to content

[SOLVED] How to display nothing if $row result is empty


rmelino

Recommended Posts

Hello,

 

This is probably an easy fix but I'm having trouble digging up the answer.

 

I have some results in my database where there is no value.  When the data is displayed in html on my page, if no result is found, i'd like there not to be anything written on the html page. 

 

Here is my code:

 

$profileid = $_GET["id"];

$connection=mysql_connect ($dbname, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}


$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}


$query="SELECT * FROM `tablename` WHERE profileid='$profileid'";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}



while ($row = @mysql_fetch_assoc($result)){

echo '<p>Name:  ' . $row['name'] . '</p>',"\n";
echo '<p>Address:  ' . $row['address'] . '</p>',"\n";
echo '<p>Email:  ' . $row['email'] . '</p>',"\n";

}						
?> 

 

 

In the example above, if say no 'email' was found for a particular record, I would want the html output to be:

 

Name:  John Doe

Address:  123 Main Street

 

INSTEAD OF:

 

Name:  John Doe

Address:  123 Main Street

Email: 

 

Any help on this would be greatly appreciated!

 

Thanks

while ($row = @mysql_fetch_assoc($result)){

echo (empty($row['name'])) ? NULL : '<p>Name:  ' . $row['name'] . '</p>' . "\n";
echo (empty($row['address'])) ? NULL : '<p>Address:  ' . $row['address'] . '</p>' . "\n";
echo (empty($row['email'])) ? NULL : '<p>Email:  ' . $row['email'] . '</p>' . "\n";

}

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.