简体   繁体   中英

search mysql database using more keywords using php

Based on caste,religion,location,gender,age i need to filter data from my database,how shall i do it.

In mysql database i have religion,gender,,location,age as separate column in same table.

Code i have used

$result=mysql_query("SELECT * FROM enquiry where location like '%$text%'and gender like'%$text1%' ")
                                                          or        die("error"); 
$found=mysql_num_rows($result); 

if($found>0){ 
 while($row=mysql_fetch_array($result)){ 
 //display of data 
  } 

Using this i can search data from one column using one keyword...as i am new please give me right way to slove this using php .

Use the OR operator

$result=mysql_query("SELECT * FROM enquiry where location like '%$text%' OR caste like '%$text%' OR religion like '%$text%' OR gender like '%$text%' OR age like '%$text%'")
                                                          or        die("error"); 
$found=mysql_num_rows($result); 

if($found>0){ 
 while($row=mysql_fetch_array($result)){ 
 //display of data 
  } 

Try creating dynamic query here is example query which you can elaborate according to your needs.Here I m assuming that you are fetching data to be searched from user via form.

Now all we have to do is check if user has filled specific field and create query accordingly.In this way your query will not be unnecessarily complex.

   $sql = "SELECT * FROM table_name ";

   if( isset($_REQUEST['location']) && $_REQUEST['location'] != '' ){
        if( strpos($sql, "WHERE") ){
             $sql .= "AND WHERE location like '%$text%'";
        }else{
             $sql .= "WHERE location like '%$text%'";
        };           
   }

   if( isset($_REQUEST['religion']) && $_REQUEST['religion'] != '' ){
       if( strpos($sql, "WHERE") ){
            $sql .= "AND WHERE religion like '%$text%'";
       }else{
            $sql .= "WHERE religion like '%$text%'";
       }
   }

   $result=mysql_query( $sql ) or die("error"); 

Try out full text search like this:-

$q="your search value";    
$searchArray = explode(" ", $q);
$query="SELECT * FROM cmusers WHERE  MATCH (`firstname`, `lastname`,`email`) AGAINST ('";
$i=0;
foreach ($searchArray as $word) {
    $query .= "*".$word."* ";
}
$query .= "' IN BOOLEAN MODE)";

this will search all key words against all columns But this need table myisam engine By default most tables are with innoDB engine so you can change engine by this

ALTER TABLE table_name ENGINE = MYISAM;

I have come across this same problem and came up with the following solution.

//Building the Query
 if (!empty($_POST['location'])) {
      $location=$_POST['location'];
      $sql[] = "enquiry.location = '%$location%'";
  }
  if (!empty($_POST['religion'])) {
      $religion=$_POST['religion'];
      $sql[] = "enquiry.religion = '%$religion%' ";
  }
  if (!empty($_POST['gender'])) {
      $gender=$_POST['gender'];
      $sql[] = "enquiry.gender LIKE '%$gender%'";
  }
    //etc...
    if (!empty($sql)) {
        $built_query = "SELECT * FROM `enquiry`";
        //This implodes the sql array creating the WHERE statement with appended AND statements
        $built_query .= " WHERE " . implode(' AND ', $sql);
    } else {
        $built_query = "SELECT * FROM `enquiry`";   
        }
    $result = mysqli_query($conn, $built_query);
    $total_records=$result->num_rows;    
    if ($total_records < 1) {
        echo "No records found";
    } else {
        while ($row_result = $result->fetch_assoc()) {  
          //Whatever you want to do with the data
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM