note 46678 deleted from function.mysql-fetch-object by sean
| From: | sean@php.net | Date: | Wed, 20 Oct 2004 03:35:45 +0000 |
| Subject: | note 46678 deleted from function.mysql-fetch-object by sean | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78801@lists.php.net to get a copy of this message | ||
Note Submitter:
----
A really fast way to return a full resultset:
<?php
/* Connect to the database and run the query... */
$result = Array();
// Read objects into our result array until we run out...
while ( $result[] =& mysql_fetch_object( $sth ) );
unset($result[count($result)-1]); // Remove the FALSE that broke the loop!
return $result;
?>
This does no unnecessary copying, and doesn't use any extra variables. The end result of the
loop will be an array of the result set plus a final FALSE, which we unset by numeric index
afterwards. Depending on your version of PHP, assigning the array member by reference rather than
relying on the default object assignment behavior will save you a LOT of processor time and memory.
After execution, you'll be ready for a foreach. If you're using PHP5, do that by
reference, too:
<?php
foreach ($result as &$data) {
doSomething($data);
}
?>
If you're in PHP4, you'll have to loop manually to access/pass by reference.
----
Reason: useless note