Jump to content

[SOLVED] How do you get the result of a count?


kjtocool

Recommended Posts

Maybe I was too quick to deem this a success, I tried to implement it into my code, and the result set is returning every matching comment, not what I requested in the limit.  The issue is not with the query, because when I run it in phpmyadmin, it returns only 10 rows.

 

Here is my code:

 

<?php
// As of right now, there is no commentStart in the URL
$comment_start = $_GET['commentStart'];

if ($comment_start == "") {
$comment_start = 0;
}

$query = "SELECT (SELECT COUNT(*) FROM Article_Comments WHERE article_ID = $article_ID) AS total_count, user_ID, comment FROM Article_Comments WHERE article_ID = $article_ID ORDER BY comment_ID LIMIT $comment_start, 10";
$result = mysqli_query($databaseConnect, $query);
$row = mysqli_fetch_assoc($result);

$num_comments = $row['total_count'];

if ($num_comments == 0) {
echo '<p class="style13">Comments <span class="style10">(There are no comments to show.) </span></p>';
}
else {
$comments_shown = $num_comments;
if ($comments_shown > 10) {
	$comments_shown = 10;
}
echo '<p class="style13">Comments <span class="style10">(Showing 1 - ' . $comments_shown . ' of ' . $num_comments . ' comments) </span></p>';
}

while ($row) {
$user_id = $row['user_ID'];
$comment = $row['comment'];

$databaseConnect2 = mysqli_connect("localhost", "user", "pass", "db")
	Or die("Unable to connect to the database.");
$query2 = "SELECT username FROM phpbb_users WHERE user_id = $user_id";
$result2 = mysqli_query($databaseConnect2, $query2);
$row2 = mysqli_fetch_assoc($result2);

$user_name = $row2['username'];

echo '<table width="70%" border="0" align="center" cellpadding="0" cellspacing="1">';
echo   '<tr>';
echo	 '<td bgcolor="#FBFBFB"><span class="style14">' . $user_name . ' says ...</span></td>';
echo   '</tr>';
echo   '<tr>';
echo	 '<td bgcolor="#FBFBFB" class="style12">' . $comment . '</td>';
echo   '</tr>';
echo '</table><br />';

mysqli_free_result($result2);
mysqli_close($databaseConnect2);
$row = mysqli_fetch_assoc($result);
} 

mysqli_free_result($result);
?>

 

I don't understand how the while ($row) comment can possibly be returning all 12 comments, when this query:

 

SELECT (

SELECT COUNT( * )

FROM Article_Comments

WHERE article_ID =31165

) AS total_count, user_ID,

COMMENT FROM Article_Comments

WHERE article_ID =31165

ORDER BY comment_ID

LIMIT 0 , 10

 

Returns only 10 rows.

That worked (though I left in the initial fetch, so it displayed starting at 1). 

 

Of course, I then went back, and uploaded the code I posted above, and of course, it worked fine this time.  Just that kind of day.  Thanks again for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.