Multi word search in PHP/MySQL

前端 未结 4 1485
故里飘歌
故里飘歌 2020-12-03 16:43

I\'m struggling to create a search that searches for multiple words. My first attempt yielded no results whatsoever and is as follows:

  require_once(\'datab         


        
4条回答
  •  余生分开走
    2020-12-03 16:48

    I've been working on the same subject (search with keywords) for a while and this how i did it :

    $words = $_POST['keywords'];
    if(empty($words)){
        //redirect somewhere else!
    }
    $parts = explode(" ",trim($words));
    $clauses=array();
    foreach ($parts as $part){
        //function_description in my case ,  replace it with whatever u want in ur table
        $clauses[]="function_description LIKE '%" . mysql_real_escape_string($part) . "%'";
    }
    $clause=implode(' OR ' ,$clauses);
    //select your condition and add "AND ($clauses)" .
    $sql="SELECT * 
          FROM functions 
          WHERE
          user_name='{$user_name}'
          AND ($clause) ";
    $results=mysql_query($sql,$connection);
     if(!$results){
        redirect("errors/error_db.html");
     }
     else if($results){
     $rows = array();
    
    

    -- Now this is how it look when i tried to run it with FULLTEXT search : But you should set the table type as "MyISAM"

    1/2(columns) => it will return nothing!(use "NATURAL LANGUAGE"="BOOLEAN")
    $sql="SELECT * FROM functions
         WHERE MATCH (function_description)
         AGAINST ('{$words}' IN NATURAL LANGUAGE MODE)";
    $results=mysql_query($sql,$connection);
     if(!$results){
        redirect("errors/error_db.html");
     }
     else if($results){
    $rows = array();
    while($rows = mysql_fetch_array($results, MYSQL_ASSOC))
    {
         // echo 
    }
    }
    ?>
    

提交回复
热议问题