in reply to What's the best way to get the first n rows, the second n rows... from a database?

I won't claim this is the best way to do it, but it's a way to do it. Update: I recommend you look at one of the other solutions.

Query the database, and put all the data in an array (I would probably make each element of the array a struct which contains all the data on a particular record). Then you can simply keep track of your location in the array (1, 49, 99, etc) and make a previous link which passes the current location -50 (if applicable) and a next link which passes the current location +50 (again, if this doesn't exceed the limits of the array). Run a loop that prints out all array entries between your current starting point and 50 more (or the end of the array, whichever comes first). Of course you need to make sure that the information in the array is available to the instance or program that prints it.

One possible problem with this approach is that your array could contain outdated information if the database is changed frequently. You could re-query the database, but that could mess up the order, unless you are ordering by a sequencial key.

  • Comment on Re: What's the best way to get the first n rows (kudra: get the whole result)