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

我已经加入了两个表images和post表,当我将两个或多个图像上传到image表post时,表标题只有一个,但它重复图像,如数字,我想知道如何解决它,任何人都可以帮助我,谢谢我的英语问题


像这样的帖子表


id  title

3   example 

图像表是这样的


id  img       post_id

1   123.jpg   3

2   22.jpg    3

3   11.jpg    3

4   21.jpg    3

5   34.jpg    3

发布标题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

}}

?>


SMILET
浏览 117回答 3
3回答

天涯尽头无女友

<?phpif ($stmt = $con->prepare("&nbsp; SELECT p.*,i.img,title,id&nbsp;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&nbsp;if($firstTime==true){echo $row['title'];$firstTime=false;}&nbsp;?><img src="images/<?php echo $row['img']; ?>"height="20"width="20"/><?php}}?>

Qyouu

列标题将重复等于图像表中返回的行数,您无法采取任何措施来阻止它。如果您按标题分组,它将返回等于帖子表中行数的行数。但如果你更详细地说明你真正想对结果做什么,我可能会帮助你。

素胚勾勒不出你

请尝试这个:-<?php$a1 = "";$stmt = $con->prepare("SELECT p.*,i.img,title,id&nbsp;&nbsp;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()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$title = $row['title'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $img = $row['img'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if($a1!= $title){&nbsp;$activity="";&nbsp;$activity.="<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>$title</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; $activity.="<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><img src="images/<?php echo $img ?>"height="20"width="20"/></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>";&nbsp; &nbsp; $a1=$title;&nbsp;&nbsp;}?>
打开App,查看更多内容
随时随地看视频慕课网APP