note 75238 rejected from function.mysql-unbuffered-query by nlopess
| From: | nlopess@php.net | Date: | Sun, 20 May 2007 13:54:03 +0000 |
| Subject: | note 75238 rejected from function.mysql-unbuffered-query by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126757@lists.php.net to get a copy of this message | ||
Note Submitter: worm at icodeviruses dot com
----
to help better understand the usefulness of this function.
<?php
# For SELECT, SHOW, DESCRIBE or EXPLAIN statements, returns array or dies
# For other statements, UPDATE, DELETE, DROP, etc, returns boolean or dies
function db_query($sql) {
db_connect();
// the results are not yet stored in memory as they would be with mysql_query()
if(!$result = mysql_unbuffered_query($sql))
die(mysql_errno()." : ".mysql_error()."\n");
if(is_resource($result)) {
$rows = array();
// here we store the results in memory for the first time
while($row = mysql_fetch_array($result, MYSQL_BOTH))
$rows[] = $row;
} else
$rows = $result;
return $rows;
}
?>
so we just cut the amount of memory needed in half to do something like:
<?php
foreach(db_query("SELECT foo FROM bar") as $value) {
// do whatever
}
?>
kinda nifty, i guess. any better examples?