echo 仅对一个 id 起作用两次,但对另一个 id 不起作用

echo works twice just for one id but doesn't for the other one

table 上有 2 个条目。但它回显了 3 个条目,但它回显了其中一个条目两次。如果我将它从 DESC 更改为 ASC,那么它会重复另一个。如果我使用 "where id <> 5" 那么它只会回显 id 6 一次。但它是一个动态站点,所以...我在另一个页面上使用了完全相同的代码并且它有效。这是完整的代码:

<?php
$cek = mysql_query('select id,isim,aciklama,tarih from galeri where dil = '.$dbDil.' order by id desc');
while($kaynak = mysql_fetch_assoc($cek)){
    $cekG = mysql_query('select resim_url from galeriresim where galeriID = '.$kaynak['id'].' order by id desc');
    $galeri .= '<h1 class="sayfaBaslik fl"><span>'.$kaynak['tarih'].'</span> '.$kaynak['isim'].'</h1>';
    $galeri .= '<h2 class="sayfaAciklama fl">'.$kaynak['aciklama'].'</h2>';
    $galeri .= '<div class="sayfaIcerik" style="width:100%">';
    $galeri .= '<div class="galeriH fl swiper-container-'.$kaynak['id'].'">';
    $galeri .= '<ul class="fl swiper-wrapper-'.$kaynak['id'].'">';
    while($kaynakG = mysql_fetch_assoc($cekG)){
           $galeri .= '<li class="swiper-slide-'.$kaynak['id'].'"><img src="'.$yol.'images/galeri/'.$kaynak['id'].'/'.$kaynakG['resim_url'].'" /></li>';
    }
    $galeri .= '</ul></div></div>';
    $galeri .='<script>';
    $galeri .= 'var mySwiper = new Swiper(\'.swiper-container-'.$kaynak['id'].'\',{';
    $galeri .= 'moveStartThreshold : 75,';
    $galeri .= 'wrapperClass : "swiper-wrapper-'.$kaynak['id'].'",';
    $galeri .= 'slideClass : "swiper-slide-'.$kaynak['id'].'"';
    $galeri .= '});';
    $galeri .= '</script>';
    echo $galeri;
}       
?>

您正在做最后的回显 在主 while() 循环中

while(..) {
   while(..) { .. }
   echo ..
}

应该是

while(..) {
   while(..) { .. }
}
echo ..

由于您在主循环中回显,因此您将 运行 回显多次,在构建时吐出 $galeri

试试这个:
我添加了 mysq_free_result 并且 echo 必须在 while 之外!

<?php
    $cek = mysql_query('select id,isim,aciklama,tarih from galeri where dil = '.$dbDil.' order by id desc');
    while($kaynak = mysql_fetch_assoc($cek)){
        $cekG = mysql_query('select resim_url from galeriresim where galeriID = '.$kaynak['id'].' order by id desc');
        $galeri .= '<h1 class="sayfaBaslik fl"><span>'.$kaynak['tarih'].'</span> '.$kaynak['isim'].'</h1>';
        $galeri .= '<h2 class="sayfaAciklama fl">'.$kaynak['aciklama'].'</h2>';
        $galeri .= '<div class="sayfaIcerik" style="width:100%">';
        $galeri .= '<div class="galeriH fl swiper-container-'.$kaynak['id'].'">';
        $galeri .= '<ul class="fl swiper-wrapper-'.$kaynak['id'].'">';
        while($kaynakG = mysql_fetch_assoc($cekG)){
               $galeri .= '<li class="swiper-slide-'.$kaynak['id'].'"><img src="'.$yol.'images/galeri/'.$kaynak['id'].'/'.$kaynakG['resim_url'].'" /></li>';
        }
        mysql_free_result($cekG);
        $cekG ="";
        $galeri .= '</ul></div></div>';
        $galeri .='<script>';
        $galeri .= 'var mySwiper = new Swiper(\'.swiper-container-'.$kaynak['id'].'\',{';
        $galeri .= 'moveStartThreshold : 75,';
        $galeri .= 'wrapperClass : "swiper-wrapper-'.$kaynak['id'].'",';
        $galeri .= 'slideClass : "swiper-slide-'.$kaynak['id'].'"';
        $galeri .= '});';
        $galeri .= '</script>';

    }    
     echo $galeri;   
    ?>

使用 JOINed 查询,那么您将不需要双 while 循环。无论如何你不应该使用 mysql 。使用 mysqli 或 pdo

'select g.id,g.isim,g.aciklama,g.tarih, gr.resim_url
  from galeri g 
  JOIN galeriresim gr on gr.galeriID = g.id
  where g.dil = '.$dbDil.' order by id desc'