PDA

View Full Version : Is there a faster way to insert records into an array


logictrap
05-10-2008, 09:30 PM
I know you can use a loop with mysql_fetch_assoc to put records into an array, but I'm wondering if there is a php/mysql command that will do this automatically as part of the query. It seems like this should be automatic since query is retrieving this already.

ie

$myarray[] = mysql_query($query, $db) or die(mysql_error());

CFMaBiSmAd
05-10-2008, 10:00 PM
Short answer is no. There is not a direct way to do that.

The result set is essentially an array that can be iterated over using one of the "fetch" functions (you can also seek any position in the set.) While there are times when putting the result set into an array has an advantage, in general, doing so just consumes additional memory (the result set is already stored in memory) and takes extra time over just accessing the result set directly.

Do you have a specific purpose for using an intermediate array?

logictrap
05-10-2008, 10:48 PM
Thanks - I am processing a fairly large email list and thought it would be faster if I could use the query result directly without having to put it into an array to process each address.

It seems like the result of a query should return an array of all the records it found automatically since in most case some type of processing is going to be performed on them.

It probably makes sense the way it works but I'm not familiar enough with the inner workings to understand it.