Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Get column names While iterating through all records

Tags:

php

mysql

$query = mysql_query("SELECT * FROM mytable");

while ($row = mysql_fetch_assoc($query)) {
   //How to echo column names and values here?
}

Is it possible to echo a table's column names and values while iterating through the entire table in a while loop?

like image 695
Norse Avatar asked Aug 01 '12 04:08

Norse


2 Answers

You can use foreach loop

$query = mysql_query("SELECT * FROM mytable");

while ($row = mysql_fetch_assoc($query)) {
     foreach($row as $key => $value) {
         print "$key = $value <br />";
     }
}
like image 163
anttix Avatar answered Oct 13 '22 13:10

anttix


$query = mysql_query("SELECT * FROM mytable");

while ($row = mysql_fetch_assoc($query)) {
    print_r($row);
}
like image 44
Omesh Avatar answered Oct 13 '22 12:10

Omesh