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

PHP多查询结果重复问题:迪拜酒店出现hotel_id 1的照片

多查询数据重复问题排查与解决

问题描述

处理多查询时遇到数据重复问题:查询酒店列表并关联对应照片时,后续酒店的结果中会包含前面酒店的照片(例如迪拜酒店结果里出现了hotel_id为1的照片)。

相关数据表截图:

原代码如下:

$query = "SELECT * FROM `hotels`";
$result=mysqli_query($connect,$query);
if(mysqli_num_rows($result)>0) {
    while($row=mysqli_fetch_array($result)) {       
        $hotelname = $row['hotel_name'];

        $queryPhotos="SELECT * FROM hotel_photo WHERE hotel_id = ".$row['id']." ";
        $resultPhotos=mysqli_query($connect,$queryPhotos);
            while($rowPhotos=mysqli_fetch_assoc($resultPhotos)) {
                        $photos[] = array(
                         "imgUrl"   =>  $rowPhotos['img_url'],
                         "hotel_id" =>  $rowPhotos['hotel_id']
                        );
            }
            
        $apiResult[] = array(
                'hotel_name' => $hotelname,
                'hotel_photos' => $photos,
            );
    }


header('Content-type: application/json');
echo json_encode($apiResult, JSON_NUMERIC_CHECK);

问题原因

$photos数组在循环外部未初始化,且每次遍历酒店时没有重置为空数组。这导致每次循环都会往同一个$photos数组中追加当前酒店的照片,最终后续酒店的hotel_photos字段会包含之前所有酒店的照片数据。

解决方案

在每次处理单个酒店的循环内部,先将$photos重置为空数组,确保每个酒店只收集自身的照片。

修改后的代码:

$query = "SELECT * FROM `hotels`";
$result=mysqli_query($connect,$query);
if(mysqli_num_rows($result)>0) {
    while($row=mysqli_fetch_array($result)) {       
        $hotelname = $row['hotel_name'];
        // 每次循环酒店时重置photos数组
        $photos = [];

        $queryPhotos="SELECT * FROM hotel_photo WHERE hotel_id = ".$row['id']." ";
        $resultPhotos=mysqli_query($connect,$queryPhotos);
            while($rowPhotos=mysqli_fetch_assoc($resultPhotos)) {
                        $photos[] = array(
                         "imgUrl"   =>  $rowPhotos['img_url'],
                         "hotel_id" =>  $rowPhotos['hotel_id']
                        );
            }
            
        $apiResult[] = array(
                'hotel_name' => $hotelname,
                'hotel_photos' => $photos,
            );
    }


header('Content-type: application/json');
echo json_encode($apiResult, JSON_NUMERIC_CHECK);

优化建议(可选)

可以通过联表查询减少数据库请求次数,提升性能,同时避免嵌套查询的潜在问题:

$query = "SELECT h.hotel_name, hp.img_url, hp.hotel_id 
          FROM hotels h 
          LEFT JOIN hotel_photo hp ON h.id = hp.hotel_id";
$result=mysqli_query($connect,$query);

$apiResult = [];
if(mysqli_num_rows($result)>0) {
    while($row=mysqli_fetch_assoc($result)) {
        $hotelName = $row['hotel_name'];
        // 按酒店名称分组整理照片
        if(!isset($apiResult[$hotelName])) {
            $apiResult[$hotelName] = [
                'hotel_name' => $hotelName,
                'hotel_photos' => []
            ];
        }
        if(!empty($row['img_url'])) {
            $apiResult[$hotelName]['hotel_photos'][] = [
                "imgUrl" => $row['img_url'],
                "hotel_id" => $row['hotel_id']
            ];
        }
    }
    // 转换为索引数组
    $apiResult = array_values($apiResult);
}

header('Content-type: application/json');
echo json_encode($apiResult, JSON_NUMERIC_CHECK);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 22:27:20