Jump to content

mysql_fetch_assoc in while loop


argh2xxx

Recommended Posts

Hello guys,

 

I'm new to php and still learning.  I'm testing this code, and my question is why mysql_fetch_assoc is acting different when it is inside while loop and when it's not.  Check the code below and see what I mean.

 

When mysql_fetch_assoc is in while loop:

echo "<p>";
$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] . " ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);

if ($numrows_comm == 0){
echo "<p>No comments.</p>";
}
else {
	echo "(<strong>" . $numrows_comm . "</strong>) comments : ";
	$i = 1;
			while($commrow = mysql_fetch_assoc($commresult)){
		echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i . "'>". $commrow['name'] . "</a> ";

		$i++;
	}
}
echo "</p>";

 

When mysql_fetch_assoc is not in while loop:

echo "<p>";
$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] . " ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);

if ($numrows_comm == 0){
echo "<p>No comments.</p>";
}
else {
	echo "(<strong>" . $numrows_comm . "</strong>) comments : ";
	$i = 1;
	$commrow = mysql_fetch_assoc($commresult);		
                        while($commrow){
		echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i . "'>". $commrow['name'] . "</a> ";

		$i++;
	}
}
echo "</p>";

 

The latter is wrong as it will print out same information repeatedly from that echo statement.  Maybe my while loop not supposed to be like that in the latter case?

Link to comment
Share on other sites

Hi,

 

mysql_fetch_assoc pulls 1 row at a time and then increments the row number - when in a while loop you constantly change the value of $commrow with the new row from your mysql_fetch_assoc statement.

 

When outside the loop you only ever pull the first row and then loop it over and over again.

 

http://uk2.php.net/mysql_fetch_assoc

 

 

Link to comment
Share on other sites

Hi,

 

mysql_fetch_assoc pulls 1 row at a time and then increments the row number - when in a while loop you constantly change the value of $commrow with the new row from your mysql_fetch_assoc statement.

 

When outside the loop you only ever pull the first row and then loop it over and over again.

 

http://uk2.php.net/mysql_fetch_assoc

 

 

 

Thanks...

Link to comment
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.