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).

But I would like to paginate the results, so I need to know what is the total number of elements in my table. 

If I perform a `COUNT(*)` in the same request, I get the number of selected elements (in my case, 10).

If I perform a `COUNT(*)` on my table in a second request, the result might be wrong because of the where, join and having clauses in my main query.

What is the cleanest way to:

* Retrieve the data
* Know the maximum number of elements in my table for this specific request

---

One solution seems to be using the [Mysql function FOUND\_ROWS](http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows) :

I tried this, as mysql\_query performs one query at a time: (taken [here](https://stackoverflow.com/a/3453823/311744))

```
$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);
}

```

And I got an array with 0 results:

```
Array ( [0] => 0 )

```

Whereas my query does returns results.

What is wrong with my approach ? Do you have a better solution ?