![](/img/trans.png)
[英]How do I use one row to pull multiple other rows from the same mySQL table
[英]Created a table with 4 columns and 3 rows. How do I set up a php script to pull each row by id to generate 3 tr from the dababase?
我要從數據庫中提取的表是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行以由單個表腳本自動生成。
您必須遍歷結果。
$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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.