简体   繁体   English

使用php使用更多关键字搜索mysql数据库

[英]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.在 mysql 数据库中,我将宗教、性别、位置、年龄作为同一个表中的单独列。

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 .使用它,我可以使用一个关键字从一列中搜索数据......因为我是新手,请给我正确的方法来使用 php 解决这个问题。

Use the OR operator使用 OR 运算符

$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这将针对所有列搜索所有关键字但这需要表myisam engine默认情况下,大多数表都使用 innoDB 引擎,因此您可以通过此更改引擎

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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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