Does mysql_num_rows recount all the rows, or does it just grab a total after a select statement?

StackOverflow https://stackoverflow.com/questions/1526931

  •  20-09-2019
  •  | 
  •  

Question

I wasn't sure if it recounted the rows, or if after it retrieved the whole result set, it only grabbed the total post query?

Was it helpful?

Solution

when you run a statement, mysql sends the row count in the header. so no, the query is not re-run to get the count.

this has an interesting implication for queries with LIMIT. mysql_num_rows() returns the number of rows returned after LIMIT is applied. if you use the SQL_CALC_FOUND_ROWS keyword in your SELECT statement, then mysql_num_rows() returns the number of rows that would have been returned if LIMIT were not used. this is helpful for paging.

OTHER TIPS

Quoting the manual literally, it “retrieves the number of rows from a result set”. It obviously doesn't run a second query if that's your question (not sure what you mean by “recount all the rows”).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top