I want to echo only the first 10 rows, but I need to count the total number of rows affected by the query.
I was doing a LIMIT 10 and then counting with the obvious problem I kept getting 10 as the count.
What would be a proper way to do it?
$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC");
$count = mysql_num_rows($data);
while($row = mysql_fetch_array( $data ))
{
echo $row['Site'];
}
MySQL has some special support for this sort of thing. First, include
SQL_CALC_FOUND_ROWSin your SELECT:Then pull out your rows and then immediately look at
FOUND_ROWS()like this:to get the number of rows that matched your original query without considering the LIMIT clause.
This is MySQL-specific but it should be a little faster than doing two queries.