rmelino Posted June 25, 2009 Share Posted June 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/163655-solved-how-to-display-nothing-if-row-result-is-empty/ Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/163655-solved-how-to-display-nothing-if-row-result-is-empty/#findComment-863491 Share on other sites More sharing options...
rmelino Posted June 25, 2009 Author Share Posted June 25, 2009 Thank you! Works perfectly... Quote Link to comment https://forums.phpfreaks.com/topic/163655-solved-how-to-display-nothing-if-row-result-is-empty/#findComment-863536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.