【问题标题】:how i can search multiple word search in Mysql Database我如何在 Mysql 数据库中搜索多个单词
【发布时间】:2015-11-29 12:26:55
【问题描述】:

>这里是代码

我想搜索数据库中的每个单词,但结果只显示单个关键字而不是多个关键字。 它是一个 PDO 数据库 当我搜索像 facebook 这样的单个词时,就会出现结果 如果我搜索两个或多个单词搜索则不会出现结果。

'

function getResults(){
 $q=$GLOBALS['q'];
 $p=$GLOBALS['p'];
 $start=($p-1)*10;
 if($p!=null){
  $starttime = microtime(true);
  $sql=$GLOBALS['dbh']->prepare('SELECT title, url, description FROM search WHERE `title` LIKE :q OR `url` LIKE :q OR `description` LIKE :q ');
  $sql->bindValue(":q", "%$q%");
  $sql->execute();
  $trs=$sql->fetchAll(PDO::FETCH_ASSOC);
  $endtime = microtime(true);
  if($sql->rowCount()==0 || $start>$sql->rowCount()){
   return 0;
  }else{
   $duration = $endtime - $starttime;
   $res=array();
   $res['count']=$sql->rowCount();
   $res['time']=round($duration, 4);
   $limitedResults=array_slice($trs, $start, 12);
   foreach($limitedResults as $r){
    $res["results"][]=array($r['title'], $r['url'], $r['description']);
   }
   return $res;
  }
 }
}
?>

'

【问题讨论】:

  • 如何传递多个单词?在 1 个变量中?
  • 你的意思是我添加了另一个变量..?
  • 您希望您的搜索搜索 facebook 并在字段中进行链接吗?如果是这样,您希望如何将它们传递给您的查询
  • 我没听懂你说什么
  • 我想做一个 PDO 查询,结果是多个查询

标签: php mysql database pdo


【解决方案1】:

不知道你为什么不使用@david strachan 的方法(我认为这是一个很好的解决方案),但我会尝试解释一个你也可能使用的选项,并理解为什么你没有得到任何结果目前。

此时发生的情况是,您将这些值发送到最终变成的查询;

SELECT title, url, description FROM search WHERE `title` LIKE `google+facebook+yahoo` OR `url` LIKE `google+facebook+yahoo` OR `description` LIKE `google+facebook+yahoo`

我不认为你想寻找包含所有这些的标题。

我不知道如何使用查询来编写此代码,但您的快速解决方案可能是这样的。

  1. 将值插入数组 (google+facebook+yahoo)
  2. 使用循环从创建的数组中选择每个键并将其传递给查询
  3. 使查询添加其结果为数组

【讨论】:

    【解决方案2】:

    您必须构建一个动态查询来搜索两个词。在下面的代码中,我说明了使用 GET 构建查询以显示查询和数组。我尽可能使用未命名的参数 ? 和“惰性”绑定 - 将数据传递给执行以方便。

    //Using GET here for testing
    
    <?php
    //Using GET here for testing
    if(isset($_GET['q'])){
    $q = $_GET['q'];
    //}
    //First split string
    if (strpos($q, ' ') > 0) {
    $pieces = explode(" ", $q);
    //echo count($pieces);  
    //You need an array to store columns
    $columnArray = array( "title", "url", "description");
    //You need an array to store parameters
    $paramArray =array();
    $clause = "WHERE";
    }
    //You need to build a dynamic query to perform this. Start with a basic stub
    
        $sql = "SELECT `title`, `url`, `description` FROM search ";
    
    //Start building the query
    //Also see PDO WIKI for use % in placeholders.ie placeholders cannot represent an arbitrary part of the query, but a complete data literal only
    //Loop through columns
            foreach ($columnArray  as $column) {
                //$sql .
                foreach ($pieces as $value) {
                    $sql .= " $clause $column LIKE ?";
                $clause = "OR";
                array_push($paramArray,"%$value%");
                }
    } 
    
    //echo query and parameter array
    echo $sql;
    echo "<br>";  
    print_r($paramArray);
    
    //Prepare and execute query 
    //  $sth = $db->prepare($sql);
    //  $sth->execute($paramArray);
    }
    ?>
    

    结果来自 xxx.php?q=google 亚马逊 quora 或 xxx.php?q=google+amazon+quora

        SELECT `title`, `url`, `description` FROM search WHERE title LIKE ? OR title LIKE ? OR title LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR description LIKE ? OR description LIKE ? OR description LIKE ?
    Array ( [0] => %google% [1] => %amazon% [2] => %quora% [3] => %google% [4] => %amazon% [5] => %quora% [6] => %google% [7] => %amazon% [8] => %quora% ) 
    

    【讨论】:

    • 感谢您的回答,但输出在浏览器中出现以下错误。从搜索中选择标题、网址、描述,title LIKE google 或 url LIKE google 数组([0] => %google% [1] => %google%)
    • 我也使用 PDO 作为结果,基本上你可以说这是某种小型搜索引擎,我使用的是 MYISAM 数据库?
    • 你试过给出的代码了吗?您似乎修改了 WHERE title LIKE ?。您必须保留 ?
    • 输出如下错误:Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
    • 这是我脚本的第一部分
    【解决方案3】:

    这是您认为有用的完整脚本。

     <?php  
                     if(isset($_GET["search"]))  
                     {  
                          $condition = '';  
                          //$query = explode(" ", $_GET["search"]);
                          $query = explode(" ", $_GET["search"]);
    
                          foreach($query as $text)  
                          {  
                               $condition .= "`title`  LIKE +'%".mysqli_real_escape_string($connect, $text)."%' OR ";  
                          }  
                          $condition = substr($condition, 0, -4);  
                          $sql_query = "SELECT * FROM countries2 WHERE " . $condition;  
                          $result = mysqli_query($connect, $sql_query);  
                          if(mysqli_num_rows($result) > 0)  
                          {  
                               while($row = mysqli_fetch_array($result))  
                               {  
                                    echo '<tr><td>'.$row["title"].'</td></tr>';  
                               }  
                          }  
                          else  
                          {  
                               echo '<label>Data not Found</label>';  
                          }  
                     }  
                     ?>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 2015-07-13
      • 2016-10-30
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多