php - 创建了一个 4 列 3 行的表。如何设置 php 脚本来按 id 提取每一行以从数据库生成 3 tr?

标签 php mysql html-table

我要从数据库中提取的表是 com/bzkItsK。它目前提取数据库中的第一行,但我不确定如何设置一个脚本,通过 ID 将所有行(当前为 4 行)提取到网页。

这是我设置的 html:

<table class="table table-striped">
   <thead>
       <tr>
          <th>user id</th>
           <th>First Name</th>
           <th>Last Name</th>
           <th>Department</th>
        </tr>
    </thead>
    <tbody>
         <tr>
            <th scope="row"><?php echo $rows[$user_id];?></th>
            <td><?php echo $rows[$first_name];?></td>
            <td><?php echo $rows[$last_name];?></td>
            <td><?php echo $rows[$dept];?></td>
            </tr>
          </tbody>
        </table>

mysql_query is as such.

mysql_connect("localhost","?","?");
mysql_select_db("?");

$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");


$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';

$rows = mysql_fetch_assoc($sql);

?>

我正在尝试按 id 拉取所有 4 行,以便由单个表脚本自动生成。

http://imgur.com/bzkItsK

最佳答案

您必须迭代您的结果。

$rows = mysql_fetch_assoc($sql);

只会获取第一个条目。 你已经做了这样的事情:

<?php while($rows = mysql_fetch_assoc($sql)): ?>
  <tr>
     <th scope="row"><?php echo $rows[$user_id];?></th>
     <td><?php echo $rows[$first_name];?></td>
     <td><?php echo $rows[$last_name];?></td>
     <td><?php echo $rows[$dept];?></td>
  </tr>
<?php endwhile; ?>

编辑: 并且请不要使用 mysql,而使用 mysqli。

关于php - 创建了一个 4 列 3 行的表。如何设置 php 脚本来按 id 提取每一行以从数据库生成 3 tr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40776360/

相关文章:

php - 将按钮值提交给 Controller 但未能发布该值

mysql - 将 'OLD' 数据库映射到新的数据库模型以与 Symfony FOSUserBundle 一起使用

javascript - 通过单击表格单元格中的任意位置来勾选表格单元格中的复选框

php - 计算上传文件的数量

php - 我可以在 ruby​​ 中复制 PHP 的 AES 加密的确切行为吗?

php - 使用发布数据进行数组搜索获取数组中的下一个元素

mysql - 查询3张表,将结果显示在一张表中

mysql - 正确的sql查询以简化关系(mysql)

html - 在表中为TR或TD分配ID是否有效?

javascript - jQuery $.each 循环中的 'each' 是什么?