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. 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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