php - 如何通过 SQL 在 PHP 中创建动态表

标签 php html mysql

我尝试制作一个动态表格。数据来自数据库。 到目前为止它有效,但我想制作一个具有单独字段的表,而不是像我的方式。 到目前为止我的代码:

 <?php

$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result); 
echo("Buy Date"."|Expire Date");
echo "<br />";
echo "<br />";
while ($row = mysql_fetch_array($result)) {
    echo '<th>'.$row['buyTime'].'</th>'."|".'<th>'.$row['untilTime'].'</th>'; 
    echo "<br />";
}
?>

结果:

Click here

那么我怎样才能制作一个正确的表格,而不是伪表格呢?

谢谢:)

问候

最佳答案

在 html 上使用 table 元素,并将 php 循环代码放入 tbody 元素内。如何正确创建表;

<table>
 <thead>
  <tr>
   <th>Buy Date</th>
   <th>Expire Date</th>
  </tr>
 </thead>

 <tbody>
 <?php
  $result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
  $num_rows = mysql_num_rows($result);

  while ($row = mysql_fetch_array($result)) {
   echo '<tr>';
   echo '<td>'.$row['buyTime'].'</td><td>'.$row['untilTime'].'</td>';
   echo '</tr>'
  }
 ?>
 </tbody>
</table>

关于php - 如何通过 SQL 在 PHP 中创建动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47112250/

相关文章:

PHP/Ubuntu-QxcbConnection : Could not connect to display aborted

php - ZendDb中类似mysql_affected_rows()的函数是什么

jquery - Bootstrap 固定菜单在滚动时损坏

php - MySQL PHP - 正斜杠后缺少字符

mysql - 多设备间数据同步

php - JMS 序列化程序。创建 2 种具有 "one-to-many"关系的模型序列化方式

PHP - 下拉列表填充但不会使用 2 个表进行选择

javascript - 数据属性中的逗号不起作用

javascript - 选择下拉菜单中的新选择时,将 CSS 类还原为原始类

javascript - 未定义的查询函数