freelancer Posted October 27, 2009 Share Posted October 27, 2009 Hello! Need some assistance here. I have table like this name, age john, 24 phil And PHP code while($row = mysql_fetch_array($result )) { echo "Name: "; echo $row['name']; echo "<br>"; echo "Age: "; echo $row['age']; echo "<br>"; } That prints all results as following: Name: John Age: 24 Name: Phil Age: How to make this happen that if there is no age in database then age row will not be displayed like so my display results should be only like this according to my data: Name: John Age: 24 Name: Phil Hope you understood Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/ Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 <?php while ($row = mysql_fetch_array ($result)) { if (!empty ($row['age'])) { echo "Name: "; echo $row['name']; echo "<br>"; echo "Age: "; echo $row['age']; echo "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945461 Share on other sites More sharing options...
Bricktop Posted October 27, 2009 Share Posted October 27, 2009 Hi Phil, You could do: while($row = mysql_fetch_array($result )) { echo "Name: "; echo $row['name']; echo "<br>"; if(!empty $row['age']) { echo "Age: "; echo $row['age']; echo "<br>"; } } mrMarcus' solution will not print the name if the age is empty. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945463 Share on other sites More sharing options...
Maq Posted October 27, 2009 Share Posted October 27, 2009 while ($row = mysql_fetch_array ($result)) { if (!empty ($row['age'])) { echo "Name: "; echo $row['name']; echo " "; echo "Age: "; echo $row['age']; echo " "; } } ?> mrMarcus, if you're going to do that, which is not what the OP wants, than you mine as well use MySQL and use a WHERE clause to check for empty age values. I don't think you can accomplish this with MySQL, but if you can that would be better. Anyway, just check if the 'age' is blank. echo ($row['age']=="") ? "" : "Age: {$row['age']} "; Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945465 Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 <?php while ($row = mysql_fetch_array ($result)) { if (!empty ($row['age'])) { echo "Name: "; echo $row['name']; echo "<br>"; echo "Age: "; echo $row['age']; echo "<br>"; } } ?> mrMarcus, if you're going to do that, which is not what the OP wants, than you mine as well use MySQL and use a WHERE clause to check for empty age values. I don't think you can accomplish this with MySQL, but if you can that would be better. Anyway, just check if the 'age' is blank. echo ($row['age']=="") ? "" : "Age: {$row['age']} <br>"; thing is, i never seem to be quite sure what the OP wants :S what you put was actually my original thought .. mistook (is that a word?) just dropping the age while keeping the name, for dropping the entire row on an empty 'age' field. Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945471 Share on other sites More sharing options...
freelancer Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks you Got it now! Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945473 Share on other sites More sharing options...
Maq Posted October 27, 2009 Share Posted October 27, 2009 thing is, i never seem to be quite sure what the OP wants :S Don't worry, me either. Quote Link to comment https://forums.phpfreaks.com/topic/179196-solved-sql-query-that-doesnt-print-empty-rows/#findComment-945491 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.