PHP MySQL获取列名称迭代所有记录

时间:2012-08-01 04:46:57

标签: php mysql

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

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

是否可以在while循环中遍历整个表时回显表的列名和值?

3 个答案:

答案 0 :(得分:14)

您可以使用foreach循环

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

while ($row = mysql_fetch_assoc($query)) {
     foreach($row as $key => $value) {
         print "$key = $value <br />";
     }
}

答案 1 :(得分:1)

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

while ($row = mysql_fetch_assoc($query)) {
    print_r($row);
}

答案 2 :(得分:-2)

试试这个.. column_name与表中使用的名称相同

echo $row['column_name'];