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

Link to comment
Share on other sites

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";

}

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.