将从数据库检索的数据显示为表头

时间:2016-05-19 08:05:47

标签: php html mysql

我无法显示我从数据库中检索到的数据库作为列标题。这是我的代码完美,但唯一的问题是它以行显示我的数据,而我希望它显示为列标题。

<?php 
  require('db.php');
  $stmt = $conn->prepare("SELECT * FROM new_emp");
  $stmt -> execute();
  $result = $stmt->fetchAll();
  foreach ($result as $row) {
  echo "<thead>";
  echo "<td>".$row['emp_name']."</td>";
  echo "<thead>";
  }

  ?>

3 个答案:

答案 0 :(得分:0)

您需要<tr>使用Table row。 对<th>使用Table Header<td>使用Table data

<?php 
  require('db.php');
  $stmt = $conn->prepare("SELECT * FROM new_emp");
  $stmt -> execute();
  $result = $stmt->fetchAll();
  foreach ($result as $row) {
  echo "<thead>";
  echo "<tr><th>".$row['emp_name']."</th></tr>";
  echo "<thead>";
  }

  ?>

答案 1 :(得分:0)

试试这个

<?php 
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
echo "<thead><tr>";  
foreach ($result as $row) {
 foreach($row as $c)
   echo "<th>".$c."</th>";
}
echo "</tr></thead>";
?>

答案 2 :(得分:0)

替换将解决您的目的。但您编写的代码将添加多个表头。如果这是你的要求,那很好。否则,您需要更改以下代码。

<?php 
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
echo "<thead><tr>";
foreach ($result as $row) {
echo "<th>".$row['emp_name']."</th></tr>";
}
echo "</th></thead>";
?>
相关问题