1

我想选择特定表中的所有列名。然后显示它们。

我搜索并得到了一些类似这样的结果。

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='yourdatabasename' 
AND `TABLE_NAME`='yourtablename';

如果我查询这个,如何获取它们并显示它们?当我获取任何表的数据时,我会根据它们的列名显示它们。

喜欢:

$table = mysql_query("SELECT * FROM table_name");

while($fetch = mysql_fetch_assoc($table))
{
  echo $fetch['id'] . ":" . $fetch['fullname'] . '<br>';
}
4

4 回答 4

2

Try this

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA WHERE TABLE_NAME = 'my_table';

You can also use

SHOW COLUMNS FROM my_table;
于 2013-04-25T11:10:07.960 回答
1
$rows = mysql_query("SHOW COLUMNS FROM table_name");

while($row = mysql_fetch_assoc($rows))
{
   echo $row['Field'];
}
于 2013-04-25T11:14:02.423 回答
0

When you mysql_fetch_assoc, you get an array where the keys are the column names:

$table = mysql_query("SELECT * FROM table_name");

while($fetch = mysql_fetch_assoc($table))
{
  foreach ($fetch as $column=>$value) {
      echo $column . ":" . $value . '<br>';
  }
}
于 2013-04-25T11:10:24.357 回答
0

尝试使用这个

for($i = 0; $i < mysql_num_fields($table); $i++) {
    $field_info = mysql_fetch_field($table, $i);
    echo "Column name: $field_info->name";
}
于 2013-04-25T11:12:59.007 回答