DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Mysql_fetch_all

04.10.2012
| 16235 views |
  • submit to reddit
        Absurdly simple but utilitarian function returns a numeric array of associative arrays containing an entire result set.

function mysql_fetch_all($result) {
    $all = array();
    while ($all[] = mysql_fetch_assoc($result)) {}
    return $all;
}
    

Comments

Snippets Manager replied on Mon, 2007/02/26 - 7:27am

class Q { function Q($requete) { global $debug,$connexion; if ($debug == '1') $this->debug = 1; if ($this->debug == '1') db($requete); $this->nb = 0; $this->V = array(); $req = mysql_query($requete,$connexion) or die('Impossible d\'exécuter la requête : '.mysql_error($connexion)); if (!@mysql_num_rows($req) || @mysql_num_rows($req) < 1) return; else { $this->nb = mysql_num_rows($req); while ($res = mysql_fetch_assoc($req)) { foreach ($res as $titre=>$value) $this->V[][$titre] = $value; } } @mysql_free_result($req); } } $S =& new Q(" SELECT * FROM galeries WHERE id='$galerie_id' LIMIT 0,10 "); for ($i=0; $i<$Q->nb; $i++) echo $Q->V[$i]['titre'];

Snippets Manager replied on Mon, 2006/10/16 - 6:57pm

Isn't this giving you one element with FALSE at the end of the array? I guess you should use: <?php function mysql_fetch_all($result) { $all = array(); while($thing = mysql_fetch_assoc($result)) { $all[] = $thing; } return $all; } ?>