Help with an IF Else statement & MySQL

This is a discussion on "Help with an IF Else statement & MySQL" within the PHP Forum section. This forum, and the thread "Help with an IF Else statement & MySQL are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 18th, 2008, 19:36
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Help with an IF Else statement & MySQL

I know this is an obvious question, but I cannot find an answer!

At the moment the page simpyl displays nothing if there are no comments in the database. I would like it to display something like 'There are no comments yet.'. This is the code for displaying the comments:
PHP: Select all

$display_comments = @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
while (
$comments mysql_fetch_array($display_comments)) {
$commentid $comments['commentsid'];
$name $comments['name'];
$comment $comments['comment'];
echo 
'<h5 class="commentsname">' $name '</h5>';
echo 
'<p class="commentstext">' $comment '</p>'
It would be something like:
PHP: Select all

IF (there are no comments) {
echo 
'<p>No comments</p>';
} else {
display comments...

Thank you!
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote

  #2 (permalink)  
Old Jan 18th, 2008, 19:43
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,650
Thanks: 0
Thanked 8 Times in 8 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Help with an IF Else statement & MySQL

Why not count the rows on your comments query??

so your IF would look like:
PHP: Select all

if($rows_count >= 1) {
//show the comments
} else {
// no comments to show!
print "Sorry, currently there are no comments to show";

Reply With Quote
  #3 (permalink)  
Old Jan 18th, 2008, 19:45
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with an IF Else statement & MySQL

Check how many rows were retrieved link this
PHP: Select all

if (!mysql_num_rows($display_comments))

         echo 
'Oh no! There\'s no comments. This is so sad for me :( ';
else {
 
// do other stuff here

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #4 (permalink)  
Old Jan 18th, 2008, 19:46
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 17
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with an IF Else statement & MySQL

PHP: Select all

$display_comments = @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
while (
$comments mysql_fetch_array($display_comments)) {
$commentid $comments['commentsid'];
$name $comments['name'];
$comment $comments['comment'];
if (
$comments == 0) {
echo 
'<p>There are no comments</p>'
} else {
echo 
'<h5 class="commentsname">' $name '</h5>';
echo 
'<p class="commentstext">' $comment '</p>'
I think?
Good luck

~Dean

Last edited by c010depunkk; Jan 19th, 2008 at 09:17. Reason: please use [PHP] tags when posting PHP
Reply With Quote
  #5 (permalink)  
Old Jan 18th, 2008, 19:50
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,650
Thanks: 0
Thanked 8 Times in 8 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Help with an IF Else statement & MySQL

PHP: Select all

<?php
$display_comments 
= @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
$rows mysql_num_rows($display_comments);
if(
$rows >= 1) {
    while (
$comments mysql_fetch_array($display_comments)) {
    
$commentid $comments['commentsid'];
    
$name $comments['name'];
    
$comment $comments['comment'];
    echo 
'<h5 class="commentsname">' $name '</h5>';
    echo 
'<p class="commentstext">' $comment '</p>'
    }
} else {
    
// no commetns to show
}
?>
Reply With Quote
  #6 (permalink)  
Old Jan 18th, 2008, 20:08
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Help with an IF Else statement & MySQL

Thanks guys. I'll try it now

Rakuli, yours works perfectly. Thanks for the help guys
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)

Last edited by Jack Franklin; Jan 18th, 2008 at 20:11.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP If Statement... mcdanielnc89 PHP Forum 16 Dec 9th, 2007 17:44
If..Else statement help IanW PHP Forum 3 Oct 6th, 2006 13:40
SELECT statement gecastill Databases 1 Feb 15th, 2006 23:27
Help with If Statement. JohnMitch Classic ASP 2 Jan 5th, 2005 01:05
With Statement Trebz Classic ASP 2 Feb 2nd, 2004 14:56


All times are GMT. The time now is 20:46.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43