Page 1 of 1

MySQL resultsets

Posted: Tue Mar 28, 2006 11:05 pm
by alex.barylski
Is there any reason why MySQL returns results as it's own special array as opposed to just a PHP multi-dimensional array?

I've never print_r()'ed a MySQL result to screen so I have no idea if it is indeed a normal array...or if it's left as a special binary resource for faster operations with the mysql_* set of functions...

In any case, what mysql_* function would yield the best performance for converting a resultset into a normal PHP array I can use in templating engines, etc???

Note: I cannot use AdoDB on this one, I know it returns PHP arrays, cuz it's worked in the past when working with template engines :)

p.s-Call me lazy, but if you could demonstrate with code how it would be done (unless it's a lengthly task or you've never done it before) I would appreciate your answer even more :)

Cheers :)

Posted: Tue Mar 28, 2006 11:55 pm
by feyd
it doesn't return a "special array" .. it returns a resource link because the data is kept in MySQL until you actually request each row.

mysql_fetch_assoc() and mysql_fetch_row() are the ones to use. fetch_row() is likely a tiny bit faster due to them being numeric arrays.

Posted: Wed Mar 29, 2006 12:40 pm
by alex.barylski
feyd wrote:it doesn't return a "special array" .. it returns a resource link because the data is kept in MySQL until you actually request each row.

mysql_fetch_assoc() and mysql_fetch_row() are the ones to use. fetch_row() is likely a tiny bit faster due to them being numeric arrays.
Thats what I meant and thought...sorta kinda :)

So I will basically have to iterate over each record and generate a replica array in PHP?

Posted: Wed Mar 29, 2006 12:51 pm
by feyd
You don't have to generate anything, MySQL's returns from the fetch commands are PHP arrays.

Code: Select all

while($data[] = mysql_fetch_row($query));
array_pop($data);
print_r($data);
If $query, the result from mysql_query(), is a valid result, $data will be a 2D array of the records. If empty, it found no records.