eRott Posted January 20, 2008 Share Posted January 20, 2008 Okay, so basically what the below code is for is some comments. It essentially is supposed to check the db where all these comments are stored. If there are no comments for that specific post, then it echo's one option, otherwise if there are comments, then it echo's another option. Problem is, while it shows the comments if there are any, it doesn't show the echo I have for if there are no comments. I would assume it has something to do with my use of mysql_num_rows. Any ideas? //the value for the variable entryid is grabbed earlier in the code $query_b = "SELECT * FROM blog_comment WHERE entry_id = '$entryid' ORDER BY id DESC"; $result_b = mysql_query($query_b, $conn) or die(mysql_error() . "<pre>$query_b</pre>"); while ($list = mysql_fetch_array($result_b)) { $chknum = mysql_num_rows($result_b); $post_date = $list['date']; $name = $list['name']; $url = $list['url']; $message = $list['comment']; if (empty($url)) { $showwho = $name; } else { $showwho = '<a href="' . $url . '">' . $name . '</a>'; } if ($post_date > mktime(0, 0, 0)) { $showdate = '<b>Today</b> ' . date("\a\\t h:i A T", $post_date); } elseif ($post_date > mktime(0, 0, 0, date("m"), date("d")-1)) { $showdate = '<b>Yesterday</b> ' . date("\a\\t h:i A T", $post_date); } else { $showdate = date("M jS, Y \a\\t h:i A T", $post_date); } if ($chknum < 1) { echo" <div id='comment-block'> <div class='message'> There are currently no comments for this entry. Feel free to be the first to <a href='blog-comment.php?entry=$entryid'>Leave A Comment</a>. </div> </div>"; } else { echo" <div id='comment-block'> <div class='header'> <div class='poster'> $showwho - $showdate </div> </div> <div class='message'> $message </div> </div> <div> </div>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/ Share on other sites More sharing options...
toplay Posted January 20, 2008 Share Posted January 20, 2008 Don't call mysql_num_rows() repeatedly inside the "while" loop like that. Call it once. Example: $chknum = mysql_num_rows($result_b); while ($list = mysql_fetch_array($result_b)) { } if ($chknum < 1) { echo" <div id='comment-block'> <div class='message'> There are currently no comments for this entry. Feel free to be the first to <a href='blog-comment.php?entry=$entryid'>Leave A Comment</a>. </div> </div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/#findComment-444079 Share on other sites More sharing options...
eRott Posted January 20, 2008 Author Share Posted January 20, 2008 Ah thanks. While I did move the mysql_num_rows() code outside of the while loop it never did anything. It never dawned on me that I had to move the if and else statements outside of the loop as well (since the variable was no longer there). Thank you again. Edit: Edited for grammar. Quote Link to comment https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/#findComment-444092 Share on other sites More sharing options...
toplay Posted January 20, 2008 Share Posted January 20, 2008 Based on the code you show, not the "else" too. Don't move that outside thew "while" loop because you're displaying $message which is obtained from $list['comment'] (rows of data inside the loop). Quote Link to comment https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/#findComment-444104 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.