Hi!
I hope I'm not writing a duplicate, but I haven't found anything that answers my question. (Although it seems to me to be quite common problem.)
The problem occurs in nearly every web project: You have a table with many many entries and want them to be displayed on single pages.
Now I wonder what's the best way to compute the number of pages needed for a certain set of table rows.
Here some approaches I've been thinking of. I'd like to get some response on how effective they are. I'll give PHP-specific examples, but I bet there are similar techniques in other languages.
The probably best way is to save the number of pages statically and modify the value every time a new entry is added. (Nevertheless... I'm looking for a dynamic solution :-) )
Do a
SELECT COUNT(*)
over the rows of interest and compute the page number every time the page is displayed.Do a ordinary select to get a result set for all rows. Now don't load the rows by calling
mysql_fetch_row
or so, but get the number of rows withmysql_num_rows
. (Since I have no idea how this is implemented I cannot say whether it is effective or not. Anyone who knows?) Then I could comfortably move the result set pointer. (Formysqli
there ismysql_data_seek
, but the native MySQL extension has no similar function. Therefore I assume that this is just some buffering behaviour ofmysqli
)
Can anyone say how to count the number of rows (number of pages) most effectively?