MySQL resultsets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

MySQL resultsets

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply