我的术语被搜索但是我的OR声明没有收到任何结果

时间:2014-06-15 13:01:37

标签: php mysql sql

session_start();

$_SESSION['word'] = $_POST['searchbar'];

$search = $_SESSION['word'];

if ($search == "") { //stopping any blank fields

echo "<p align=center><b>Please Enter Something To Search For In The Search Box.</b></p>";

}
else
{

$result = mysql_query("SELECT * FROM clothes WHERE (`title` LIKE '%".$search."%') || (`description` LIKE '%".$search."%')") or die(mysql_error()); //trying to do a statement that asks for a search to be done on TITLE and DESCRIPTION for what ever term someone has searched for so I can show them the results

echo "<p align=center>You Searched For $search</p>"; //showing the term that is being searched

if($result[0] == 0) //checking to see if the results come up as NONE then the following message to be shown
{
echo "<p align=center><b><font size=3>No Results Were Found.</font></b></p>";
}
else
{

//如果该术语有一些结果,则会显示以下表格

echo "results shown here";

}
}

我遇到的问题是我的OR声明没有收到任何结果。

搜索字词显示,搜索没有OR语句,但是一旦我尝试在TITLE和DESCRIPTION列中搜索它不喜欢它的结果。

任何帮助?

1 个答案:

答案 0 :(得分:0)

session_start();

$_SESSION['word'] = $_POST['searchbar'];

$search = $_SESSION['word'];

if ($search == "") {

echo "<p align=center><b>Please Enter Something To Search For In The Search Box.</b></p>";

}
else
{

 $searchTerms = explode(' ', $search);
 $searchTermBits = array();
 foreach ($searchTerms as $term) {
 $term = trim($term);
 if (!empty($term)) {
    $searchTermBits[] = "description OR title LIKE '%$term%'";
 }
 }


  $result = mysql_query("SELECT * FROM clothes WHERE ".implode(' OR ', $searchTermBits)."") or die(mysql_error());


  echo "<p align=center>You Searched For $search</p>";


   $journey = mysql_num_rows($result);

if ($journey == 0) {

echo "<p align=center><b>There Were No Results. Please Try Again</b></p>";

}

else

{

while($resultr=mysql_fetch_assoc($result))

echo "result here";

  }

}

改变了一点,它仍然读取了两个区域,但没有像之前那样搜索精确的术语,而是有人可以搜索“特大号男士T恤”,只要那是男士T恤就会出现在“标题”或“男性或T恤或大型或额外的术语”等描述中说明。