如何防止标题在 php mysqli 中重复

How to prevent title repeating in php mysqli

我加入了两个 table imagespost table 当我将两个或更多图像上传到 image [=32] 时,我的脚本出现问题=] post table 标题只有一个,但它重复数字之类的图像我想知道如何解决它任何人都可以帮助我谢谢我的英语问题

Post table 像这样

id  title
3   example 

图片table 像这样

id  img       post_id
1   123.jpg   3
2   22.jpg    3
3   11.jpg    3
4   21.jpg    3
5   34.jpg    3

Post 标题 one 但它重复了 5 time 张图片我想阻止它

这是我的源代码

<?php
if ($stmt = $con->prepare("  SELECT p.*,i.img,title,id 
from post AS p LEFT JOIN images AS i ON i.post_id= p.id ")) {
$stmt->execute();
}
$result = $stmt->get_result();
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
?>
<?php echo $row['title']; ?>
<img src="images/<?php echo $row['img']; ?>"height="20"width="20"/>
<?php
}}
?>
<?php
if ($stmt = $con->prepare("  SELECT p.*,i.img,title,id 
from post AS p LEFT JOIN images AS i ON i.post_id= p.id ")) {
$stmt->execute();
}
$result = $stmt->get_result();
if ($result->num_rows>0) {
$firstTime=true;
while ($row = $result->fetch_assoc()) {
?>
<?php 
if($firstTime==true){
echo $row['title'];
$firstTime=false;
}
 ?>
<img src="images/<?php echo $row['img']; ?>"height="20"width="20"/>
<?php
}}
?>

列标题将重复return图像中的行数table,您无法阻止它。

如果您按标题分组,它将 return 行数等于 post table.

中的行数

但是,如果您详细说明您实际想要对结果做什么,我可能会帮助您。

请试试这个:-

<?php
$a1 = "";
$stmt = $con->prepare("SELECT p.*,i.img,title,id  
from post AS p INNER JOIN images AS i ON i.post_id=p.id");
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
           $title = $row['title'];
            $img = $row['img'];
               }
               
 if($a1!= $title){
 $activity="";
 $activity.="<tr>
            <td>$title</td>
            </tr>;
            }
    $activity.="<tr>
            <td><img src="images/<?php echo $img ?>"height="20"width="20"/></td>
                      </tr>";
    $a1=$title; 
 }
?>