Jump to content

Recommended Posts

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>";

	}

}

Link to comment
https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/
Share on other sites

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>";

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/#findComment-444079
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/86867-solved-mysql_num_rows/#findComment-444092
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.