References and memory efficiency
| From: | Joachim Krebs | Date: | Sat, 01 Nov 2003 13:43:00 +0000 |
| Subject: | References and memory efficiency | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-168267@lists.php.net to get a copy of this message | ||
Hi there,
I've been working on a database wrapper class for a while now, MySQL to be specific. Until now, I've simply had a fetch function that returned an array of all the rows that the database returned, only because I have gotten so tired of always writing the same while loop to iterate through all the rows.
However, I have discovered that the method I'm using, passing around large multidimensional arrays of text by copy, is extremely memory inefficient, so I am working on a new method. Tell me if this is any better:
Rather than using something like:
$sql = 'SELECT text, date, etc FROM news WHERE blah=\'foo\'';
if (!$myquery = $db->query($sql)) { echo 'uh oh'; }
if (!$db->numrows($myquery)) { echo 'no rows found'; }
$news = $db->fetchNews($myquery);I could do something like:
$news = array();
$db->fetch($myquery, $news);
Where fetch($query, &$array) is the header. In the second case, the fetch function would therefore write the rows directly to the array which was passed as a reference rather than returning a copy. Am I right in thinking that this is a better method? Should I shut up and do a while loop?
Thanks,
Joachim