tags:

views:

91

answers:

4

Please help in php code

$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
foreach($data as $row){                            

            foreach($row as $key=>$value){

                echo $key;
                echo $value;

            }
+3  A: 

Check the manual page for mysql_query - it returns a resource handle, not an array of results. You use the resource handle with functions like mysql_fetch_assoc() to get each row of the result set.

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

    //process $row
}
mysql_free_result($rs);
Paul Dixon
A: 

here you go

$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;
 } 
}

Cheers

RageZ
A: 

You should do a mysql fetch array to fetch all the results.

$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;
    }
}
Damien MATHIEU
you missed a ) on the while
RageZ
A: 

If you want to use foreach on a mysql_query result you'll have to implement an iterator. For example:

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 )
{
    // ...
}
Jon Benedicto