473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about displaying the results of a search

I have this question:
I have a simple search to a db, something like:
"select description from games where year = '1990'"
suppose I get 300 results, I would like to display this in pages of 30
results per page, so I would use "limit 0, 30" for the first page.
But this way, I don't know how many results would I have if I didn't use
"limit".

So how do I know if I have more than one page to display?, the only way I
can thing is to do the fist query without limit just to know the number of
results and the second one with "limit". But this seems like a lot of
overhead just to know the total results, is there a more efficient way to
do it?
Thank you.

Francisco
Jul 17 '05 #1
6 1991
Francisco <fr*****@mailandnews.com> wrote:
I have this question:
I have a simple search to a db, something like:
"select description from games where year = '1990'"
suppose I get 300 results, I would like to display this in pages of 30
results per page, so I would use "limit 0, 30" for the first page.
But this way, I don't know how many results would I have if I didn't
use "limit".

So how do I know if I have more than one page to display?, the only
way I can thing is to do the fist query without limit just to know the
number of results and the second one with "limit". But this seems like
a lot of overhead just to know the total results, is there a more
efficient way to do it?


SELECT COUNT(*) AS mycount FROM games WHERE year = '1990'

Is very quick.

JOn
Jul 17 '05 #2
Francisco <fr*****@mailandnews.com> wrote:
I have this question:
I have a simple search to a db, something like:
"select description from games where year = '1990'"
suppose I get 300 results, I would like to display this in pages of 30
results per page, so I would use "limit 0, 30" for the first page.
But this way, I don't know how many results would I have if I didn't use
"limit".

So how do I know if I have more than one page to display?, the only way I
can thing is to do the fist query without limit just to know the number of
results and the second one with "limit". But this seems like a lot of
overhead just to know the total results, is there a more efficient way to
do it?
Thank you.


It depends on how complex the query is. As someone else suggested, you
can use count(), thats fairly quick for simple queries or cases where
your database is capable of deriving the answer quickly. (this sounds
like the approach that would work best for you)

If it's a complex query (with potentially thousands of results) things
get a lot more complicated. Most of the time I see people doing a "Next"
page (use Limit 0,31 and skip over the last result to see if you have
another page)

That still has a problem if the query is _complex_ because the database
will still have to iterate over thousands of rows when it's time to do a
LIMIT 5000 .. (this is not the case where the database can use indices
and stuff, but for LIKE('%query%'), or relations where the database
would have to sort on a temporary result or something, indices might not
be usable)

If it's really complex you could cache the results of the full query and
use the cache. It all depends on what your query is and how many hoops
you're willing to go through to get at it. (cache is fast but it can
result in having large files and/or give headaches with multiple web
servers, additional bugs etc.) I've never used the cache approach with
PHP, only perl it typically involves a fork & wait for the 'n' results
to become available.

Jamie
Jul 17 '05 #3
Thank you very much, I'll use the approach that Jon sugested since my db is
small, but it's good to know the other alternatives.

Francisco
Jul 17 '05 #4
I noticed that Message-ID: <Xn**************************@130.133.1.4>
from Jon Kraft contained the following:
SELECT COUNT(*) AS mycount FROM games WHERE year = '1990'


As this is a PHP group shouldn't you be recommending mysql_num_rows() ?
:-)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
Uzytkownik "Francisco" <fr*****@mailandnews.com> napisal w wiadomosci
news:c3*************@ID-99245.news.uni-berlin.de...
Thank you very much, I'll use the approach that Jon sugested since my db is small, but it's good to know the other alternatives.


No! That approach is only needed when you use a brain-dead DB like MSSQL
Server. MySQL is optimized for such operation. See Geoff Berrow's post.
Jul 17 '05 #6
Geoff Berrow <bl******@ckdog.co.uk> wrote:
I noticed that Message-ID: <Xn**************************@130.133.1.4>
from Jon Kraft contained the following:
SELECT COUNT(*) AS mycount FROM games WHERE year = '1990'


As this is a PHP group shouldn't you be recommending mysql_num_rows() ?
:-)


I would if it didn't return the number of rows returned by the query,
which would be 30 ;)

JOn
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim T | last post by:
Hi, I have a stored procedure executing a search and an asp.net page displaying the results in a datagrid. The datagrid has paging on it, I am using Visual Studio.NET and can't see any option...
1
by: C Downey | last post by:
I have a web page that I displays the search results from another form. The way I have it developed right now, my data access layer returns me the results as an array (first name, lastname, id). ...
1
by: Wee Bubba | last post by:
i am querying a database and displaying rows of search results on my web page. the first column of each row contains a <linkbutton>. when a user clicks this <linkbutton> i want to take some values...
1
by: David Miller | last post by:
Dear Sir or Madam: I am writing an application in VB.Net, and I have an interesting threading situation that I cannot seem to figure out. I have a search class (CSearch) that is responsible for...
5
by: temp2 | last post by:
Hello, I have an app that reads data params from a stream and updates controls accordingly. The stream reader is on a different thread than the main thread that created the controls. I fully...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
7
by: orderofblessing | last post by:
Hi every one: I just registered on forum today to get the help on a little but tricky problem to me. Actually i am converting a php website into .NET C#. I just want to display each search...
5
by: L C | last post by:
Hello, Could someone point in the correct direction with regards to displaying my data in "nicer" format. I have figured out ( with the tutorials on the web, and the O'Reilly PHP & MySQL book)...
2
by: NasirMunir | last post by:
I have created a table in access (copied from excel). Then I created a form which contains a text field and a list box. The text field is actually a look-up field, where a user can enter a searchable...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.