我试图回显数据库中我的职位表上的所有内容,但似乎只显示一个,如果我尝试复制代码,它将仅重复职位表中1个ID中的一个
<div class="row">
<?php
$query = "SELECT * FROM products";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$product_id = $row['product_id'];
$product_category_id = $row['product_category_id'];
$product_name = $row['product_name'];
$product_price = $row['product_price'];
$product_details = $row['product_details'];
$product_image = $row['product_image'];
?>
<div class="col-sm-6 col-lg-4">
<div class="single_special_cource">
<img width="360px" height="313px" src="img/<?php echo $product_image; ?>" class="special_img" alt="">
<div class="special_cource_text">
<a href="course-details.php?p_id=<?php echo $product_id; ?>" class="btn_4">
<?php
$query = "SELECT * FROM categories WHERE cat_id = {$product_category_id}";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo $cat_title;
}
?>
</a>
<h4><?php echo $product_price; ?></h4>
<a href="course-details.php?p_id=<?php echo $product_id; ?>">
<h3><?php echo $product_name; ?> </h3>
</a>
<p><?php echo $product_details; ?> </p>
</div>
</div>
</div>
<?php
}
?>
</div>
答案 0 :(得分:0)
尝试在循环到时更改
$row = mysqli_fetch_assoc($result);
while($row) {
// code here
$row = mysqli_fetch_assoc($result);
}
答案 1 :(得分:0)
在主while循环中使用的变量遍历了您的行和查询。更改内部循环变量名称
<?php
$categoryQ = "SELECT * FROM categories WHERE cat_id = {$product_category_id}";
$categoryRes = mysqli_query($connection, $categoryQ);
while ($row2 = mysqli_fetch_assoc($categoryRes)) {
$cat_id = $row2['cat_id'];
$cat_title = $row2['cat_title'];
echo $cat_title;
}
?>
例如