By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

sql query results and scrolling

P: n/a
I make a mySQL database query and display the first 10 results. Then
the user can click on "next" link to get the next 10 results. How can
I do that without issuing the sql query again? Can I keep the results
from the first query?

Jens Martin Schlatter
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jens Martin Schlatter wrote:
I make a mySQL database query and display the first 10 results. Then
the user can click on "next" link to get the next 10 results. How can
I do that without issuing the sql query again? Can I keep the results
from the first query?


Yes, but I think this is a bad idea.
Save the results from the first query in a session variable.
It is *much* ( _very_ _much_ ) better to get *only* what you need from
the database in the first place.

First query
select ... limit 0, 10

and then, based on the page number the user wants to see
select ... limit ($page_number-1)*10, 10
Check out the MySQL manual for SELECT
http://www.mysql.com/doc/en/SELECT.html

--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
Jul 17 '05 #2

P: n/a
> > I make a mySQL database query and display the first 10 results. Then
the user can click on "next" link to get the next 10 results. How can
I do that without issuing the sql query again? Can I keep the results
from the first query?


It is *much* ( _very_ _much_ ) better to get *only* what you need from
the database in the first place.


Thank you very much for your good hint.
Do you think it is less overhead to do a query for each page?

Jens Schlatter

Jul 17 '05 #3

P: n/a
Jens Martin Schlatter wrote:
Do you think it is less overhead to do a query for each page?


Really I don't know. If you're already using sessions *and* the table is
small (and will not grow) ??????

Anyway, you will always have to do some processing on every page; either
selecting from the array stored in a session variable or having the DB
do it for you.

Which reminds me: DO use a SORT BY in the queries with a LIMIT clause!

Never measured the difference between opening (and closing) a file and
opening (and closing) a connection to the database -- but I expect the
database thing to be faster :-)
--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.