Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

echoing user comments

Explorer ,
Mar 20, 2012 Mar 20, 2012

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. 

TOPICS
Server side applications
726
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 21, 2012 Mar 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.

Translate
LEGEND ,
Mar 21, 2012 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2012 Mar 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));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 21, 2012 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2012 Mar 21, 2012
LATEST

ok, seems to work.  i thought you had to insert "$sql_row = mysql_fetch_array($sql_result);" first because how would it know what a "$sql_result" is since the do statement is done first?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines