note 46678 added to function.mysql-fetch-object
| From: | php-general at lists dot php dot net | Date: | Wed, 20 Oct 2004 01:17:14 +0000 |
| Subject: | note 46678 added to function.mysql-fetch-object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78799@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-object.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46678
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46678&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46678&report=yes
Search -- http://master.php.net/manage/user-notes.php