Jump to content

Mysql number of rows returned


c_pattle

Recommended Posts

Thank you.  Also I have another question.  At the moment this is my mysql query

 

$data_sql = "select * from data limit 0, 24";

 

However I want to get the number of all of the rows in the table "data".  There for should I run a the sql ="select * from data" and then use mysql_num_rows on the results of that query and store it in a variable?  However surely this is a bad way of doing it because I am selecting all of the data from a database just to get the number of rows. 

Or, if you have 5.0+ use

select SQL_CALC_FOUND_ROWS * from data limit 0, 24

 

followed by another query

 

SELECT FOUND_ROWS()

 

The second query will return the number of rows that would have satisfied the previous query if the LIMIT was not present.

 

$sql = "select SQL_CALC_FOUND_ROWS * from data WHERE categoryID = 4 limit 0, 24";
$resList = mysql_execute($sql);
$resCnt = mysql_execute("SELECT FOUND_ROWS()");
$row = mysql_fetch_row($resCnt);
$totalAvailable = $row[0];

while ($row = mysql_fetch_assoc($resList)) {
  // Show the data
}

echo "There are " . $totalAvailable . " records available for Category 4";

(I just threw the WHERE clause in to show that it can be used)

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.