LiamH Posted July 23, 2009 Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/ Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881329 Share on other sites More sharing options...
LiamH Posted July 23, 2009 Author Share Posted July 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881336 Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881339 Share on other sites More sharing options...
seventheyejosh Posted July 23, 2009 Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881341 Share on other sites More sharing options...
LiamH Posted July 23, 2009 Author Share Posted July 23, 2009 That works thanks. I needed to slightly change your code to get the else message to display. The else needed to go between the end if and end while } Quote Link to comment https://forums.phpfreaks.com/topic/167145-solved-else-if/#findComment-881353 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.