ranell
ranell

Reputation: 703

Number total of result with limit

I'm trying to optimise the number of SQL request. I've found that i've used twice the same request like that :

//a big select without limit
$select="select * from tab1, tab2 ,tab 3 where (jointure..)";

// return to me 20 line
$nbtotal=mysql_num_rows(mysql_query($select));
// I apply the limit for my paging system 5 per page
$select.=" limit  ".$offset.",".$limit;

// all the information that i need to fetch
$result_of_myrequest=mysql_query($select);

I've tried count(article.id) but it return to me a big number in each count!

can i combine in the same request (with limit 0,5) the $nbtotal AND the results of my request ??

Many thanks!

Upvotes: 0

Views: 146

Answers (1)

MatthewMcGovern
MatthewMcGovern

Reputation: 3496

Run the limit query which will return you your results to show, then you can call another query:

SELECT FOUND_ROWS();

Which will return the total number of rows from the previous query whilst ignoring the limit.

//a big select with limit
$select="select * from tab1, tab2 ,tab 3 where (jointure..) limit ".$offset.",".$limit;

// all the information that i need to fetch
$result_of_myrequest=mysql_query($select); 

// return to me 20 line
$nbtotal = mysql_query("SELECT FOUND_ROWS()");

Upvotes: 3

Related Questions