PHP - 使用搜索栏时重复的结果

时间:2012-12-07 12:27:50

标签: php search duplicates rows

$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each){
    $x++;
    if($x==1) 
        $construct .="concat_ws(Name, Summary, Description, OpeningHours, Address, Postcode, Keywords) LIKE '%$search_each%' ";
    else
        $construct .="AND concat_ws(Name, Summary, Description, OpeningHours, Address, Postcode, Keywords) LIKE '%$search_each%' ";
}

$construct ="SELECT * FROM Rating, Attraction WHERE $construct" ;
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);

if ($foundnum==0)
    echo "Sorry, there are no matching result for '$search'" ;
else
{
    echo "";
    while($row = mysql_fetch_assoc($run)){
        echo '<a href="details.php?ID=' . $row['AttractionID'] . '"><img class="attractionimg" src="'. $row['ImageUrl'] . '" height="160" width="210" /></a>';
        echo '<div class="a-textcontain">';
        echo '<h2><a href="details.php?ID=' . $row['AttractionID'] . '">' . $row['Name'] . '</a></h2>'; 
        echo '<p class="a-text">' . $row['Summary'] . '</p>';
        echo '</div>';
        echo '<img class="rating" src="'. $row['RatingUrl'] . '" height="25" width="140" alt="Rating"/>';
        echo '<div class="aborderline"></div>';
        break;
    }
}  

我的数据库中有12行数据。当我搜索它正常工作但重复每个搜索结果12次。我尝试过'休息';在上面你可以看到,但这会将搜索限制为只有一个结果......

有人有什么想法吗?


$ construct =“SELECT Attraction。*,Type.TypeName,Rating.RatingUrl”;

$ construct。=“FROM Attraction”;

$ construct。=“INNER JOIN Type ON Attraction.Type = Type.TypeID”;

$ construct。=“INNER JOIN评级ON Attraction.AttractionID = Rating.AttractionID”;

$ construct。=“WHERE Attraction = $ construct”;

1 个答案:

答案 0 :(得分:1)

SELECT * FROM Rating, Attraction

您正在进行多表查询,其中Rating的每一行都会连接到Attraction的每一行,并将结果相乘。您将需要具有特定连接条件的LEFT JOIN查询或可能的其他内容,具体取决于意图。

请参阅http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.htmlhttp://dev.mysql.com/doc/refman/5.1/en/join.html