473,836 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What happens exactly when i do a db query?

128 New Member
Hello i'm intrigued by what happens behind the scenes when I do a DB query to a database from php.

I would like to know what is the exact life cycle of a mysql_query()

I ask this question because i need to know what is the best solution to count the number of rows from a query.

My query is performed to fetch records from a mysqlDb that will be displayed as a list of let's say 10 rows. If i hadn't put a sql
Expand|Select|Wrap|Line Numbers
  1. LIMIT start, count
the query would have returned more rows.

So my second question is:
"How can i know "what would have been the total amount of rows from a query if i hadn't specified the LIMIT clause?" Although i need to specify the LIMIT because i don't want to pass to php 1000000000 hundred k records if i'm only going to use 10.

1. I thought that i should use mysql_affected_ rows(), but this will return 10 (determined by the LIMIT clause)

2. I thought of doing 2 queries one without the LIMIT and then call mysql_affected_ rows() and the second query with the LIMIT and then call mysql_fetch_arr ay().
(that is why i wanted to know how mysql_query() function works. I wanted to know if there was a lot of overhead even if I didn't call mysql_fetch_arr ay() for the first query)

3. I thought of querying once without LIMIT and then loop with mysql_fetch_arr ay() and only save the records in php that i want.
(this is ok if i want record from 0 to 9 or even 100 to 109 but what if i want the last records: 10 000 to 10 009 ?)

Well now that you know what my life is all about, I hope you can figure a good solution.

Thank you for reading this far


Oct 10 '09 #1
4 1865
8,658 Recognized Expert Moderator Expert
with a little help from my sql:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) FROM `db_table`
Oct 10 '09 #2
128 New Member
Thank you Dormilich!

And you are sure that it is faster than querying without limit and then calling mysql_affected_ rows() ?

If yes, how do I handle the resultset? Like with a normal query without the while?
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) AS numRows FROM ... JOIN ... GROUP ... ";
  2. $row = mysql_fetch_array($resultset));
  3. $numRecords = $row['numRows'];
like that?
Oct 10 '09 #3
8,658 Recognized Expert Moderator Expert

you get a single value as result, so you wonít need a loop

I donít think you need that AS, since you expect a single value (tho Iím not an SQL expert)
Oct 10 '09 #4
128 New Member
ok Thank's a lot !
Oct 10 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I write some PHP code which should, I think, be able to to auto create the tables. The SQL looks like this:
by: Warren Wright | last post by:
Hi All, First, where can I get some questions of this sort answered? Preferably, are there good books or online guides that I can consult for these types of answers when necessary? 1. How do I know, when executing a query from Query Analyzer or otherwise, how many temporary tables will be needed, and how big they will be? 2. Where will those temporary tables be created? Always in tempdb?
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what happens if the number of lines is huge? I have a very big file (4GB) I want to read in, but I'm sure there must be some limitation to readlines and I'd like to know how it is handled by python. I am using it like this:
by: visionstate | last post by:
Hi there, Is there a way of searching a sub-form (reading from a query) without writing what is exactly contained in the query field. For example if I had a 'Finance Manager' in the query but just wanted to type 'Finance' in the text box to search or 'Admin Manager' in the query and just wanted to search 'Admin' in the text box. How would I do it so when I searched just 'Admin' or 'Finance', 'Admin Manager' and 'Finance Manager' appeared...
by: Xah Lee | last post by:
in March, i posted a essay ‚ÄúWhat is Expressiveness in a Computer Language‚ÄĚ, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
by: clintonG | last post by:
Every single time neophytes or converts ask about naming and style conventions what are they told by the majority consensus? The answer is "do what you prefer but do so consistently" right? Yes, that's right, that's exactly what is said. People joining a team of developers are also encouraged to cooperate with their peers and adopt what has already been established right? Yes, that's exactly what is encouraged. Teamwork and cooperation...
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I delete a row, only the child record is deleted from the source tables; the parent record is still there...which is what I wanted. Now display fields from that query in a continuous form. When I delete a record from that form, one of the child...
by: Pranav | last post by:
class just{ public : int x; just(){ x=1234; } just(int i){ x = i;} ~just(){ cout << " Here We Are\n" ;} }; int main() {
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.