Re: [PHP] Getting mysql_query results into an array



On Tue, February 13, 2007 5:34 pm, Skip Evans wrote:
I read on php.net about resources, the type
returned from mysql_query(), and was trying locate
the best way to get the result set back from a
query into an array.

Is simply looping through the result set with
mysql_fetch_assoc() the common way to do this?

As great as PHP is with arrays I was wondering if
there is some simply, more efficient way, but as
yet have not located one.

The most efficient way is "Don't do that." :-)

Simply loop through the results and do whatever you want to do with
them, and don't put them into an array at all.

This question has appeared before, and usually breaks down to one of
these scenarios:

#1
loop through mysql result set to build $array
loop through $array to output results

In this case, it's clearly more efficient to do:
loop through mysql result set to output results



#2
loop through mysql result set to build $array
perform some kind of calculation upon $array

In this case, it's USUALLY much more efficient to write an SQL query
to perform the calculation.

Databases are highly optimized for this kind of thing, and very
efficient at it.
PHP is a generalized programming language, and not so efficient for
this kind of task.


#2 does occasionally have an exception to the rule, where the SQL
query is nasty and the PHP is fast and easy, but that's awfully rare.


--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
.