Why not give me the DB query result directly?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Why not give me the DB query result directly?

Post by wvoyance »

I just don't understand why does DB query does not give me the result directly, but need one more step of fetch_array?
Is there situation we might need the intermediate result?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Why not give me the DB query result directly?

Post by mikosiko »

Huh?...... elaborate... with code
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why not give me the DB query result directly?

Post by califdon »

The result of a query can be anything from one value (such as the count of the no. of records in a table) to a 2-dimensional array of a million records with 200 columns in each record. The fetch operations fetch one record at a time so that you can display or do something else with them. It's not an intermediate result.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: Why not give me the DB query result directly?

Post by wvoyance »

califdon wrote:The result of a query can be anything from one value (such as the count of the no. of records in a table) to a 2-dimensional array of a million records with 200 columns in each record. The fetch operations fetch one record at a time so that you can display or do something else with them. It's not an intermediate result.
Sorry, I mean sqli_query or similar command is an intermediate result.
The query need always be done in two steps. Why?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why not give me the DB query result directly?

Post by califdon »

wvoyance wrote:
califdon wrote:The result of a query can be anything from one value (such as the count of the no. of records in a table) to a 2-dimensional array of a million records with 200 columns in each record. The fetch operations fetch one record at a time so that you can display or do something else with them. It's not an intermediate result.
Sorry, I mean sqli_query or similar command is an intermediate result.
The query need always be done in two steps. Why?
That is exactly what I just answered. The query is not an intermediate result, it IS the result, the WHOLE result. Usually you want to fetch individual rows of the result to do something with them, which requires a fetch from the result, one row at a time.
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: Why not give me the DB query result directly?

Post by wvoyance »

califdon wrote:
wvoyance wrote:
califdon wrote:The result of a query can be anything from one value (such as the count of the no. of records in a table) to a 2-dimensional array of a million records with 200 columns in each record. The fetch operations fetch one record at a time so that you can display or do something else with them. It's not an intermediate result.
Sorry, I mean sqli_query or similar command is an intermediate result.
The query need always be done in two steps. Why?
That is exactly what I just answered. The query is not an intermediate result, it IS the result, the WHOLE result. Usually you want to fetch individual rows of the result to do something with them, which requires a fetch from the result, one row at a time.

O.K. You said it is the result. Yes, the WHOLE result. But it is put somewhere else, isn't it?
Morrison's book told me the result is actually an ID, and said if I use echo I will get
Resource id #3 or something like that.
If it give me in array, it is also the WHOLE result.
Why don't it just gave me an array? I can get individual rows as I want too.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Why not give me the DB query result directly?

Post by califdon »

wvoyance wrote:O.K. You said it is the result. Yes, the WHOLE result. But it is put somewhere else, isn't it?
Morrison's book told me the result is actually an ID, and said if I use echo I will get
Resource id #3 or something like that.
If it give me in array, it is also the WHOLE result.
Why don't it just gave me an array? I can get individual rows as I want too.
That's exactly what a result IS: an array of data. It's not "put somewhere else". The variable you assign the result to is the handle so that you CAN get the individual rows. That's precisely what a query is all about. You need to read book explanations of details like what is echoed back, with an understanding of what the big picture is. With any database and any language, an SQL query is a request to the database engine to selectively return data to the program and that is precisely what it does, in the form of an array. Typically it is an array with many rows and many columns, which you can't do much with, as a whole array of data, so you use one of the fetch commands to step through the rows, one at a time. Get this idea of "intermediate result" or "put somewhere else" out of your mind, it is just confusing you and it's entirely inaccurate.
Post Reply