gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:09:57 25 4
gpt4 key购买 nike

我要从数据库中提取的表是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com