Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the number of results MySql would have returned without limit?
    primarykey
    data
    text
    <p>I have a table with a lot of data, so I retrieve it and display it one page at a time (my request is lengthy so there is no way I run it on the entire table).</p> <p>But I would like to paginate the results, so I need to know what is the total number of elements in my table. </p> <p>If I perform a <code>COUNT(*)</code> in the same request, I get the number of selected elements (in my case, 10).</p> <p>If I perform a <code>COUNT(*)</code> on my table in a second request, the result might be wrong because of the where, join and having clauses in my main query.</p> <p>What is the cleanest way to:</p> <ul> <li>Retrieve the data</li> <li>Know the maximum number of elements in my table for this specific request</li> </ul> <hr> <p>One solution seems to be using the <a href="http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows" rel="nofollow noreferrer">Mysql function FOUND_ROWS</a> :</p> <p>I tried this, as mysql_query performs one query at a time: (taken <a href="https://stackoverflow.com/a/3453823/311744">here</a>)</p> <pre><code>$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users'; $result = mysql_query($query); // fetching the results ... $query = 'SELECT FOUND_ROWS()'; $ result = mysql_query($query); // debug while ($row = mysql_fetch_row($result)) { print_r($row); } </code></pre> <p>And I got an array with 0 results:</p> <pre><code>Array ( [0] =&gt; 0 ) </code></pre> <p>Whereas my query does returns results.</p> <p>What is wrong with my approach ? Do you have a better solution ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload