Never been to DZone Snippets before?

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

mysql_fetch_all (See related posts)

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 on this post

albert006 posts on Jan 31, 2007 at 06:34
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;
}

?>
molokoloco posts on Feb 26, 2007 at 06:36

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'];

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts