2009-09-16 2 views

Répondre

3

Vérifiez la page de manuel pour mysql_query - il renvoie une poignée de ressources, pas un tableau de résultats. Vous utilisez le handle de ressource avec des fonctions telles que mysql_fetch_assoc() pour obtenir chaque ligne du jeu de résultats.

$rs= mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error()); 
while ($row = mysql_fetch_assoc($rs)) { 

    //process $row 
} 
mysql_free_result($rs); 
0

ici vous allez

$result = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error()); 
while($data = mysql_fetch_array($result, MYSQL_ASSOC)){ 
foreach($data as $key => $value){ 
    echo $key; 
    echo $value; 
} 
} 

Vive

1

Vous devriez faire un mysql chercher tableau pour chercher tous les résultats.

$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error()); 
while ($row = mysql_fetch_array($data)) { 
    foreach($row as $key=>$value){ 
     echo $key; 
     echo $value; 
    } 
} 
+0

vous avez raté a) sur le moment – RageZ

0

Si vous voulez utiliser foreach sur un résultat mysql_query, vous devrez implémenter un itérateur. Par exemple:

class MySQLResultIterator implements Iterator 
{ 
    private $result; 
    private $current; 
    private $pos = 0; 

    public function __construct($result) 
    { 
     $this->result = $result; 
     $this->updateCurrent(); 
    } 

    public function rewind() { 
     mysql_data_seek($this->result, 0); 
     $this->pos = 0; 
     $this->updateCurrent(); 
    } 

    public function current() { 
     return $this->current; 
    } 

    public function key() { 
     return $this->pos; 
    } 

    public function next() { 
     $this->updateCurrent(); 
     $this->pos++; 
     return $this->current; 
    } 

    public function valid() { 
     return ($this->current != false); 
    } 

    private function updateCurrent() { 
     $this->current = mysql_fetch_assoc($this->result); 
    } 
} 

$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error()); 

$iterator = new MySQLResultIterator($data); 
foreach($iterator as $row) 
{ 
    // ... 
} 
Questions connexes