Altamash Altamash - 1 month ago 8
MySQL Question

how i can search multiple word search in Mysql Database

> here is the code.


i want to search the every word in my database but the results only show with single keyword not the multiple.
it is a PDO database
when i search single word like facebook then the result is appear
if i search two or more word search then the result is not appear.


'

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;
}
}
}
?>


'

Answer

here is complete script you found helpful.

 <?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>';  
                      }  
                 }  
                 ?>  
Comments