2

基于种姓、宗教、位置、性别、年龄,我需要从我的数据库中过滤数据,我该怎么做。

在 mysql 数据库中,我将宗教、性别、位置、年龄作为同一表中的单独列。

我用过的代码

$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 
  } 

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

4

4 回答 4

0

尝试这样的全文搜索:-

$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)";

这将针对所有列搜索所有关键字但这需要表myisam engine 默认情况下,大多数表都带有innoDB引擎,因此您可以通过此更改引擎

ALTER TABLE table_name ENGINE = MYISAM;
于 2014-04-28T10:43:54.087 回答
0

尝试在此处创建动态查询是示例查询,您可以根据需要进行详细说明。这里我假设您正在通过表单从用户那里获取要搜索的数据。

现在我们要做的就是检查用户是否填写了特定字段并相应地创建查询。这样您的查询将不会不必要地复杂。

   $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"); 
于 2013-10-29T11:36:38.933 回答
0

使用 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 
  } 
于 2013-10-29T11:28:39.030 回答
0

我遇到了同样的问题并提出了以下解决方案。

//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
        }
于 2020-10-01T21:59:02.807 回答