我有一个表,该表显示数据库表的结果,并且数据库中有文件数量结果的单元格。看起来像这样
<?php
include ('../../include/dbConnect.php');
include ('../include/header.php');
include ('../include/checkAdmin.php');
$sql = "SELECT * from categories";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
if($count > 0)
?>
<div class="text">
<h2>Categoties</h2>
<form class="action" action="add.php" method="POST">
<button type="submit" class="btn" name="add_category">Add Category</button>
</form></div>
<br>
<table>
<thead>
<tr>
<th width= "300"><a href='?order=category'>Category</a></th>
<th colspan="3" width= "150">Action</th>
<th width= "150">Number of files</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result -> fetch_assoc()): ?>
<tr>
<td><?php echo $row['category']; ?></td>
<td>
<form action='edit.php?id=<?php echo $row['id']; ?>' method="post">
<button type="submit" class="btn" name="edit_btn" >Edit</button></form></td>
<td>
<form action='list.php?id=<?php echo $row['id']; ?>' method="post">
<button type="submit" class="btn" name="list_btn" >Files</button></form> </td>
<td>
<form action='delete.php?id=<?php echo $row['id']; ?>' method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<input type="submit" name="submit" class="btn" value="Delete">
</form></td>
<td><?php $query = "SELECT * from files WHERE category_id =" . $row['category'];
$res = mysqli_query($connection,$query);
$r = mysql_num_rows($res);
**echo**
?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table><br>
<?php include('../include/footer.php');
?>
我想回显每个类别中文件的数量。如您所见,这些表与category_id相关。
答案 0 :(得分:1)
使用COUNT获取行数。
您可以使用下面给出的查询来解决问题-
$query = "SELECT count(*) from files WHERE category_id =" . $row['category'];
答案 1 :(得分:0)
<td><?php $query = "SELECT count(*) from files WHERE category_id =" . $row['category'];
$res = mysqli_query($connection,$query);
$r = mysql_num_rows($res);
echo $r;
?></td>
我正在获取mysql_num_rows()期望参数1是资源,给定布尔值