Posted By

sorehead on 10/09/06


Tagged

fetch mysql all


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
esritter


Mysql Fetch All


 / Published in: PHP
 

Simple but utilitarian function returns a numeric array of associative arrays containing an entire result set.

  1. <?php
  2. /*
  3. mysql_fetch_all
  4.  
  5. Absurdly simple but utilitarian function returns a numeric array of associative arrays containing an entire result set.
  6. */
  7. function mysql_fetch_all($result) {
  8. $all = array();
  9. while ($all[] = mysql_fetch_assoc($result)) {}
  10. return $all;
  11. }
  12. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: zaphod on April 25, 2007

the while loop would be better written as

while ($row = mysql_fetch_assoc($result)){ $all[] = $row; }

otherwise, the last (false) fetch will give you 1 extra array item

Posted By: Xunnamius on March 30, 2012

An even better version that I've been using for a few years:

function mysqlfetchall($res) { $a = array(); while($a[] = mysqlfetchassoc($res)); array_pop($a); return $a; }

also deals with the one-off issue

You need to login to post a comment.