如何在单个 php 文件中使用 fetch_assoc() 两次?
How to user fetch_assoc() twice in a single php file?
我有两个table。一个是文章列表,另一个是类别列表。在类别 table 中,我使用 fetch_assoc() 从我的数据库中获取数据并在 table 中列出。但是我想在文章table中获取文章的数据。如何在同一个 php 文件中两次使用 fetch_assoc()?
<table>
<caption class="table_title">Category Management</caption>
<thead>
<tr class="table_header">
<th>ID</th>
<th>Ttile</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while($row = $categoryResult->fetch_assoc()) {?>
<tr class="table_content">
<td><?php echo escape($row['id'])?></td>
<td><?php echo escape($row['title'])?></td>
<td><a href="./update_category.php?id=<?php echo escape($row['id'])?>">Edit</a></td>
<td><a href="./handle_delete_category.php?id=<?php echo escape($row['id'])?>">Delete</a></td>
</tr>
<?php }?>
</tbody>
</table>
文章table
<tbody>
<?php while($row = $result->fetch_assoc()) {?>
<tr class="table_content">
<td></td>
<td></td>
<td></td>
<td><a href="./update.php">Edit</a></td>
<td><a href="./delete.php">Delete</a></td>
</tr>
<?php }?>
</tbody>
替换
while($row = $categoryResult->fetch_assoc()) {
和
foreach($categoryResult as $row)
它做同样的事情,但 foreach
方法使用一个自动迭代器,它总是从结果集的开头开始。
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
如果出于某种原因,您必须使用 while
循环和手动方法,那么您需要确保在开始循环之前始终将内部指针重置为第一条记录。但是,不建议手动循环。使用 while
手动执行此操作时更容易犯更多错误
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}
我有两个table。一个是文章列表,另一个是类别列表。在类别 table 中,我使用 fetch_assoc() 从我的数据库中获取数据并在 table 中列出。但是我想在文章table中获取文章的数据。如何在同一个 php 文件中两次使用 fetch_assoc()?
<table>
<caption class="table_title">Category Management</caption>
<thead>
<tr class="table_header">
<th>ID</th>
<th>Ttile</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while($row = $categoryResult->fetch_assoc()) {?>
<tr class="table_content">
<td><?php echo escape($row['id'])?></td>
<td><?php echo escape($row['title'])?></td>
<td><a href="./update_category.php?id=<?php echo escape($row['id'])?>">Edit</a></td>
<td><a href="./handle_delete_category.php?id=<?php echo escape($row['id'])?>">Delete</a></td>
</tr>
<?php }?>
</tbody>
</table>
文章table
<tbody>
<?php while($row = $result->fetch_assoc()) {?>
<tr class="table_content">
<td></td>
<td></td>
<td></td>
<td><a href="./update.php">Edit</a></td>
<td><a href="./delete.php">Delete</a></td>
</tr>
<?php }?>
</tbody>
替换
while($row = $categoryResult->fetch_assoc()) {
和
foreach($categoryResult as $row)
它做同样的事情,但 foreach
方法使用一个自动迭代器,它总是从结果集的开头开始。
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
如果出于某种原因,您必须使用 while
循环和手动方法,那么您需要确保在开始循环之前始终将内部指针重置为第一条记录。但是,不建议手动循环。使用 while
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}