【发布时间】:2019-08-03 06:40:54
【问题描述】:
美好的一天!当我从两个具有相同值的表中选择数据时,我遇到了重复结果的问题。我的第一个表homepost 中有一个数据,ID 为2,我在表multiple 中有三个数据,ID 为2。
所以。在我的源代码中。我正在选择具有相同值ID 的所有数据。但它复制了我来自homepost 的一个数据,因为我有来自multiple 的三个具有相同值[ID] 的数据。有什么帮助吗?谢谢!
例如下表:
Table [homepost] Table [multiple]
homeID homeDesc multipleID multipleImage
2 John 2 Image1
3 Samantha 2 Image2
2 Image3
3 Image4
3 Image5
我的代码结果:
John
Image1
John
Image2
John
Image3
Samantha
Image4
Samantha
Image5
我想要的是:
John
Image1
Image2
Image3
Samantha
Image4
Image5
这是我的源代码:
<?php
include ("dbconnect.php");
$content_sql="SELECT post.postID, post.postTitle, post.postDesc, post.postImage
FROM post
JOIN category ON (post.categoryID=category.categoryID)
WHERE post.categoryID=".$_GET['categoryID'];
if($content_query=mysqli_query($con, $content_sql)) {
$content_rs=mysqli_fetch_assoc($content_query);
}
do {
echo $content_rs['postTitle'];
echo $content_rs['postDesc'];
} while ($content_rs=mysqli_fetch_assoc($content_query))
?>
【问题讨论】:
-
您的脚本对SQL Injection Attack 甚至if you are escaping inputs, its not safe! 都是开放的,在
MYSQLI_或PDOAPI 中使用prepared parameterized statements -
我现在才刚刚开始了解 PHP 的工作原理,因为我只是一个初学者。我认为安全性稍后才会出现。
-
您可以使用 group_concat ,它会以逗号分隔的形式返回所有 multipleImage 名称。看看stackoverflow.com/questions/13451605/…
标签: php sql join select duplicates