回显所有字段名称及其各自的值

时间:2011-11-02 09:22:10

标签: php mysql html

我想从mysql数据库中的表打印一个包含所有字段名称及其值的列表。我如何使用循环打印:

<table>
<tr>
<td>
FIELD NAME
</td>
<td>
FIELD VALUE
</td>
</table

任何帮助将不胜感激

更新

第一个字段是unique_id,我如何跳过第一个字段?说从第二个字段回显所有

3 个答案:

答案 0 :(得分:1)

这是非常基本的PHP,简单的while循环与foreach相结合就足够了:

echo '<table>';
$query = mysql_query("SELECT * FROM `your_table`");
// Loop over all result rows
while($row = mysql_fetch_assoc($query)) {
    // Loop over all fields per row
    foreach($row as $field => $value) {
        echo '<tr><td>' . htmlentities($field) . '</td><td>' . htmlentities($value) . '</td></tr>';
    }
    // New data row can optionally be seperated with a blank line here
    echo '<tr><td colspan="2">&nbsp;</td></tr>';
}
echo '</table>';

答案 1 :(得分:0)

使用mysql_field_name()mysql_num_fields()mysql_fetch_field()mysql_field_seek()

keys(mysql_fetch_assoc())

答案 2 :(得分:0)

这是我几天前做的事情:

mysql_connect('localhost','user','password');
mysql_select_db('database');

$sql = "SELECT * FROM  your_table";
$result = mysql_query($sql);

echo "<table border='1'>";
echo "<tr>";
$i = 0;
while ($i < mysql_num_fields($result))
{
    $meta = mysql_fetch_field($result, $i);   
    echo "<th>".$meta->name."</th>";
    $i++;
}

while ($row = mysql_fetch_row($result))
{
    echo '<tr>';

    foreach($row as $item)
    {
        echo "<td>".$item."</td>";
    }

    echo '</tr>';
}

echo "</table>";
相关问题