You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从数据库中仅展示指定house_id的图片而非全部图片

enter image description here

实现方案

你只需要在查询数据库的SQL语句中添加WHERE条件,筛选指定house_id的记录即可,具体修改方式如下:

固定筛选house_id=4的场景

直接修改原SQL查询语句,把原来的代码行:

$query = $db->query("SELECT * FROM images2 ORDER BY house_id DESC");

替换为:

$query = $db->query("SELECT * FROM images2 WHERE house_id = 4 ORDER BY house_id DESC");

修改后查询只会返回house_id为4的图片,幻灯片也就只会展示对应id的内容。

支持动态切换house_id的优化方案

如果后续需要根据页面参数灵活切换展示不同house_id的图片,推荐使用预处理语句避免SQL注入风险,完整修改后的代码如下:

<?php
// 引入数据库配置文件
include_once 'dbConfig.php';
// 要查询的house_id,可根据实际需求修改来源,比如从URL参数获取:$houseId = $_GET['house_id'] ?? 4;
$houseId = 4;
// 预处理查询避免SQL注入
$stmt = $db->prepare("SELECT * FROM images2 WHERE house_id = ? ORDER BY house_id DESC");
$stmt->bind_param("i", $houseId);
$stmt->execute();
$query = $stmt->get_result();
?>


<div class="frame">
            <div class="slideshow-container">
            <?php if($query->num_rows > 0){
              while($row = $query->fetch_assoc()){

                $imageURL = 'uploads2/'.$row["file_name"];?>
                <div class="mySlides fade">
            <img style="height: 200px;" src="<?php echo $imageURL; ?>">
             </div><?php }?>
             
             <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
             <a class="next" onclick="plusSlides(1)">&#10095;</a>
        </div></div>

内容的提问来源于stack exchange,提问作者Gauthier VANLIER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:06:08