Connecting Tech Pros Worldwide Help | Site Map

What happens exactly when i do a db query?

bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#1: Oct 10 '09
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_array().
(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_array() for the first query)

3. I thought of querying once without LIMIT and then loop with mysql_fetch_array() 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

Regards,

bilibytes
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,626
#2: Oct 10 '09

re: What happens exactly when i do a db query?


Quote:

Originally Posted by bilibytes View Post

"How can i know "what would have been the total amount of rows from a query if i hadn't specified the LIMIT clause?"

with a little help from my sql:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) FROM `db_table`
bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#3: Oct 10 '09

re: What happens exactly when i do a db query?


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'];
  4.  
like that?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,626
#4: Oct 10 '09

re: What happens exactly when i do a db query?


Quote:

Originally Posted by bilibytes View Post

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

yes

Quote:

Originally Posted by bilibytes View Post

If yes, how do I handle the resultset? Like with a normal query without the while?

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

Quote:

Originally Posted by bilibytes View Post

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) AS numRows FROM ... JOIN ... GROUP ... ";

I don’t think you need that AS, since you expect a single value (tho I’m not an SQL expert)
bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#5: Oct 10 '09

re: What happens exactly when i do a db query?


ok Thank's a lot !
Reply