Skip to main content
Known Participant
March 21, 2012
Answered

echoing user comments

  • March 21, 2012
  • 1 reply
  • 718 views

I am having trouble with retreiving user comments.  here is my code:

if(mysql_fetch_array($sql_result) == 0) {

    echo "There are currently no comments to display. Be the first to post one!";

} else {

echo "<table width=\"100% border=2\">";

echo "<tr>";

echo "<td><b>User</b></td>

<td><b>Date/Time</b></td>

<td><b>Comment</b></td>";

echo "</tr>";

while($sql_row=mysql_fetch_array($sql_result))

$comment=$sql_row["comment"];

$date=date('D, d M Y H:i:s', $sql_row["time"]);

$user = findUserName($sql_row["user"]);

$imgAvatarSrc = "AVATAR IMAGE SOURCE";

?>

SOME HTML HERE

<br />

  <div style="float:right;"><div class="replyBtn"><a href="#btn_comment">Reply</a></div></div>

</div>

</div>

<div id="padding" style="padding:15px;"></div>

<?php

echo "<tr><td><a href='view_profile.php?user=$user' target='_blank' class='noUnderline'><img src='" . $imgAvatarSrc ."' width=65px height=65px/> " . $user . "</a><br></td></tr>";

echo "<tr><td>".$date."</td>";

echo "<td>".$comment."</td>";

}

echo "</tr></table>";

}

if there are no comments related to this image, it shows "be the first to post a comment!"  i can post in the else when there is a comment, but then in the while, nothing is printed out.  not sure where i am going wrong here... i tried removing the "else" statement, and made the while it's statement, and it worked fine. 

This topic has been closed for replies.
Correct answer bregent

mysql_fetch_array moves the data pointer ahead. You've used it in the first statement to check if there are rows. When you use it again in your WHILE loop, it's already been moved ahead. Use a do-while loop instead.

1 reply

bregentCorrect answer
Participating Frequently
March 21, 2012

mysql_fetch_array moves the data pointer ahead. You've used it in the first statement to check if there are rows. When you use it again in your WHILE loop, it's already been moved ahead. Use a do-while loop instead.

Known Participant
March 21, 2012

I am having trouble getting the row values:

$sql="Select * from comments WHERE fileRef = '$file'";

$sql_result = mysql_query($sql,$connection1)

    or exit("Sql Error: " . mysql_error());

$sql_num = mysql_num_rows($sql_result);

if(mysql_fetch_array($sql_result) == 0) {

    echo "There are currently no comments to display. Be the first to post one!";

} else {

do { 

$sql_row = mysql_fetch_array($sql_result);

$comment=$sql_row["comment"];

?>

html

<?php

} while($sql_row = mysql_fetch_array($sql_result));

}

Participating Frequently
March 21, 2012

You've still go the same problem. You are trying to fetch the values after you have already moved the pointer ahead.

Try.

$sql="Select * from comments WHERE fileRef = '$file'";

$sql_result = mysql_query($sql,$connection1)

    or exit("Sql Error: " . mysql_error());

$sql_num = mysql_num_rows($sql_result);

if(!$sql_row = mysql_fetch_array($sql_result)) {

    echo "There are currently no comments to display. Be the first to post one!";

} else {

do { 

$comment=$sql_row["comment"];

?>

html

<?php

} while($sql_row = mysql_fetch_array($sql_result));

}

Alternatively, you could just test the value in $sql_num to see if any comments where posted, rather than using mysql_fetch_array. Then you could use your WHILE loop as originally posted. The key here is that once you call mysql_fetch_array, you must echo the results before calling it again.