Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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));
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more