Page 1 of 1
Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 6:59 pm
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?
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 7:12 pm
by mikosiko
Huh?...... elaborate... with code
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 7:32 pm
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.
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 7:47 pm
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?
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 8:05 pm
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.
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 8:53 pm
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.
Re: Why not give me the DB query result directly?
Posted: Thu Jun 28, 2012 9:30 pm
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.