Jump to content

Recommended Posts

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!

 

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.

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']} 
";

<?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.

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.