我有以下代码,效果很好。但是,我仅限于搜索两行。我想再搜索两行(共4行)。我尝试过几种技术,但是我无法使用它。有时我会得到重复的结果。
<?php
$term = $_POST['term'];
$connect = new mysqli("localhost", "root", "hala3ammi", "phprealty");
$sql = "
SELECT phprealty_property.*, phprealty_prop_img.p_id, phprealty_prop_img.fn
FROM phprealty_property
INNER JOIN phprealty_prop_img
ON phprealty_property.id = phprealty_prop_img.p_id
WHERE phprealty_prop_img.def='1'
AND (phprealty_property.title like '%$term%'
OR phprealty_property.full_desc like '%$term%')";
$query = mysqli_query($connect, $sql) or die (mysqli_error());
$result = mysqli_query($connect, $sql);
$found = mysqli_num_rows($result);
echo '<br/> Search result(s): '.$found;
while ($row = mysqli_fetch_array($result)){
$img = $row["fn"];
$thumb = 'th_' . $img;
$duh = '<img src="../falcon/listImgs/' . $thumb . '" />';
echo '<br/> Title: '.$row['title'];
echo '<br/> Price: '.$row['price'];
echo '<br/> Type: '.$row['type'];
echo '<br/> Image: '.$duh;
echo '<br/> Full Desc: '.$row['full_desc'];
echo '<br/><br/>';
}
?>
数据库:
phprealty
我要搜索的表格:
phprealty_property
INNER联接用于从下表中检索默认缩略图。
phprealty_prop_img
WHERE phprealty_prop_img.def='1'
提前谢谢。
答案 0 :(得分:1)
我认为您可能获得看似重复的结果的原因是您的内部联接。如果phprealty_prop_img中有n行对应于phprealty_property中的单个属性列表(即phprealty_prop_img中的n行具有相同的值p_id),那么您将获得查询返回的n行,每行都具有从phprealty_property中提取的相同信息,并且仅在phprealty_prop_img.fn中有所不同。请查看W3C网站http://www.w3schools.com/sql/sql_join_inner.asp
上给出的内部联接示例关于通过phprealty_property.city&amp;搜索phprealty_property.short_desc,我不明白这个问题。为什么你不能在这个结尾处将这些添加到OR子句中?
AND (phprealty_property.title like '%$term%'
OR phprealty_property.full_desc like '%$term%'
OR phprealty_property.city LIKE '%$city%'
OR phprealty_property.short_desc LIKE '%$term%')