我已經編寫了代碼,它將根據查詢的結果集動態生成一個表。每行中的每個字段都放在它自己的單元格中。如何將列名作爲標題添加到適當的列上?從數據庫獲取名稱列上面的表
我的代碼:
$db = new mysqli("...", "...", "", "...");
$query = "SELECT * from customer ";
if ($result = $db->query($query)) {
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
printf("%s\n", $finfo->name);
}
$result->close();
}
$db = new mysqli('...', '...', '...', '...');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * from ...";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table class='table'>";
while($row = $result->fetch_assoc()){
echo "<tr class='info'>
<td>" . $row['COLUMN1'] . "</td>
<td>" . $row['COLUMN2'] . "</td>
<td>" . $row['COLUMN3'] . "</td>
<td>" . $row['COLUMN4'] . "</td>
<td>" . $row['COLUMN5'] . "</td>
<td>" . $row['COLUMN6'] . "</td>
</tr>";
}
echo "</table>";
?>
------------圖片問題解決了-------------
您是否試圖顯示整列? –
你的循環內只有4個'',而在你的圖像中有6列? – Akshay
使用'SELECT * from customer'來獲取列名是非常糟糕的。至少在查詢上添加一個LIMIT 1。 – Ray