php - 我的术语已被搜索,但我的 OR 语句没有收到任何结果

标签 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 语句即可工作,但当我尝试在标题和描述列中搜索结果时,它不喜欢它。

有什么帮助吗?

最佳答案

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 恤或大号或特大条款”等中注明。

关于php - 我的术语已被搜索,但我的 OR 语句没有收到任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229840/

相关文章:

php - JSON + MySQL 查询

MySql 存储过程选择表

php - 时间格式转换使用php

sql - Oracle 10g 中的聚合字符串连接

php - 如何使用mysqli bind_result

php - { } 在字符串中做什么?

java - Java 中的 XSD 到 DBMS

c# - Linq to Entities Distinct 从句

PHP MySQL 使用变量进行查询

mysql - GROUP BY 显示所有不同的列