PHP搜索函数返回将数字作为默认值的结果

时间:2014-01-01 11:26:59

标签: php search

我目前在我的网站上搜索功能时遇到我的php代码问题。由于数组,它会继续返回标题,描述或关键字中包含数字的结果(以及搜索结果)。

有没有更好的方法来获取结果而不返回包含数据库中数字的默认结果?

直播示例http://www.proimagehub.com/videolist.php向右搜索。

<?php

  if(isset($_POST['search'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[ a-zA-Z0-9]+/", $_POST['sitesearch'])){
  $words = mysql_real_escape_string($_POST['sitesearch']);   
$arraySearch = explode(" ", protect($words));       
$countSearch = count($arraySearch);
$a = 0;
$query = "SELECT * FROM tutorials WHERE ";
$quote = "'";
while ($a < $countSearch)
{
  $query = $query."keywords || description || title LIKE $quote%$arraySearch[$a]%$quote ";

  $a++;
  if ($a < $countSearch)
  {
    $query = $query." || ";
    }   
  }

  //-run  the query against the mysql query function
  $result=mysql_query($query) or die(error);
  $counters = mysql_num_rows($result);
    echo "<h3>Search Results for: ".$words."</h3>";
    echo "<h5>Total Matching Results: ".$counters."</h5>";  

  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
            $Keywords=$row['keywords'];
            $Description=$row['description'];
            $Title=$row['title'];
            $ID=$row['id'];
            $Rank = $row["rank"];
            $Type = $row["type"];
            $Image = $row['img'];
            $ImageNumber = $row['imgnum'];
            $Video = $row['video'];
  //-display the result of the array
?>

1 个答案:

答案 0 :(得分:0)

主要问题是查询部分

... WHERE keywords || description || title LIKE '...'

未按预期工作。 ||logical OR operator。它不是某种语义OR运算符,它会以您希望的方式自动与LIKE运算符组合。

您必须在各个字段上使用LIKE运算符,例如:

WHERE
    keywords LIKE '...' OR
    description LIKE '...' OR
    title LIKE '...'