Jump to content

Recommended Posts

Hi all.

 

I thought I had this sussed, but it appears not.

 

I have a page where I want some information to be displayed if it is present in the database. If there is no data then I want it to display something else.

 

This is what I have so far, but it's not working;

 

if (mysql_num_rows($res1) > 0) {

while($row = mysql_fetch_assoc($res1)) {

echo "".$row['Title']."<br>";
}
		if {
			echo "morning";
		}
}

 

I've also tried it with elseif, but same result. The error is that it's unexpected. So evidently what I want to do can't be done this way. How can it be done? Thanks

Link to comment
https://forums.phpfreaks.com/topic/167145-solved-else-if/
Share on other sites

if what? the reason it's giving you an error is because there's no condition to go with that if.

 

if you mean you want to display "morning" when there are no matching rows, then you need to use else to compliment the first if():

 

if (mysql_num_rows($res1) > 0) {

while($row = mysql_fetch_assoc($res1)) {

echo "".$row['Title']."<br>";
}
}
else
{
  echo 'morning';
}

Link to comment
https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881329
Share on other sites

Thanks. That makes more sense. I've just realised I missed a bit of code when I typed the original post. It should actually read;

 

if (mysql_num_rows($res1) > 0) {

while($row = mysql_fetch_assoc($res1)) {

echo "Title: ".$row['Title']."<br>";
}
}
else
{
  echo 'morning';
}

 

I'm not getting the error anymore, but it's still printing "Title:" even though there is no data in the DB.

Link to comment
https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881336
Share on other sites

Thanks. That makes more sense. I've just realised I missed a bit of code when I typed the original post. It should actually read;

 

if (mysql_num_rows($res1) > 0) {

while($row = mysql_fetch_assoc($res1)) {

echo "Title: ".$row['Title']."<br>";
}
}
else
{
  echo 'morning';
}

 

I'm not getting the error anymore, but it's still printing "Title:" even though there is no data in the DB.

 

then either your query is incorrect, or 'Title' isn't actually a column you're selecting in the query, because MySQL is finding rows somehow.

Link to comment
https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881339
Share on other sites

try

 


if (mysql_num_rows($res1) > 0) {

while($row = mysql_fetch_assoc($res1)) {

	if($row['title']!=''){

	echo "Title: ".$row['Title']."<br>";

		}//end if

	}//end while

}else{
  
	echo 'morning';

	}//end if

 

that should only print title if there is a variable to print out. not sure what the top # rows is checking so i just left it for you

Link to comment
https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881341
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.