如何使用MATCH / AGAINST创建简单的搜索?

时间:2016-05-03 16:48:49

标签: php mysql

我已将我的mysql表从InnoDB修改为MyISAM,然后我使用这句话添加了FULLTEXT:

ALTER TABLE个人ADD FULLTEXT(个人姓名,姓氏,缩写,电子邮件,电话,adegree);

在我的应用程序中,我有6个输入字段,如下所示:

- id -personal_name -姓 -initials -电子邮件 - 电话

和1选择这样的字段: 的 -adegree

如您所知,我必须根据这些字段检索信息,那么我如何使用MATCH / AGAINST?如果不能使用MATCH / AGAINS进行多重搜索,我可以用其他任何方式实现。

我使用了%LIKE%句子,但是效果不好,这是代码:

file:searchUser.php

(文档)$。就绪(函数(){

$("#search").on("click",function(e){
    e.preventDefault();

     $.ajax({

                       dataType:'html',
                       type: "POST",
                       url: "processarSearchUser.php",
                       data: dades , 

                       success: function(data){

                   $("#contenidor").show("500",function(){

                    $("#contenidor").html(data);


                            })

                 }//succes
             });//ajax

    });//search

    });//document



<form name="formulariFilter" id="formulariFilter"  method="post" enctype="multipart/form-data">
                <h1>Search User</h1>     
                  <tr>   
                    <td><b>id:</b></td>
                    <td><input type="text" name="id" id="id"></td>
                  </tr>  

                   <tr>  
                    <td><b>Personal name:</b></td>
                    <td><input type="text" name="personalname" id="personalname" ></td>
                  </tr>  

                   <tr>  
                    <td><b>Surname:</b></td>
                    <td><input type="text" name="surname" id="surname" ></td>   
                  </tr>  

                   <tr>  
                    <td><b>Initials:</b></td>
                    <td><input type="text" name="initials" id="initials" ></td>
                  </tr> 

                  <tr>   
                    <td><b>Email:</b></td>
                    <td><input type="text" name="email" id="email" ></td>
                  </tr>  

                  <tr>   
                    <td><b>Telephone:</b></td>
                    <td><input type="text" name="telephone" id="telephone" ></td>
                  </tr>  

                  <tr>   
                    <td><b>Academic degree:</b></td>


                        <td><select name="adegree" id="adegree">
                                <option value="0">---Select something---</option>
                            <?php 
                                    $consulta= mysqli_query($conexio, "SELECT adegree from personal" ); 
                                    while($fila=mysqli_fetch_assoc($consulta)){
                                    echo"<option value='".$fila['adegree']."'/>"."<b>".$fila['adegree']."</b>"."</option>";

                                       }
                                ?>
                        </select></td>
                    <!--<td><input type="text" name="adegree" id="adegree" ></td>-->
                  </tr>   
                  <tr>
                    <td colspan="2" id="td_boto"><input type="button"  name="search"  id="search" value="SEARCH" ></td>

                  </tr>                             
            </form>

file:processarSearchUser.php

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    include("../Projecte Bonita/conectar.php");


 $conexio=conectar_bd();
     $id = mysqli_real_escape_string($conexio,addslashes($_POST['id']));
     $personalname = mysqli_real_escape_string($conexio,addslashes($_POST['personalname']));
     $surname = mysqli_real_escape_string($conexio,addslashes($_POST['surname']));
     $initials = mysqli_real_escape_string($conexio,addslashes($_POST['initials']));
     $email = mysqli_real_escape_string($conexio,addslashes($_POST['email']));
     $telephone = mysqli_real_escape_string($conexio,addslashes($_POST['telephone']));
     $adegree = mysqli_real_escape_string($conexio,addslashes($_POST['adegree']));




/*$sql="SELECT  applus_code,personal_name,surname,initials,email,telephone,adegree from personal  WHERE  
        id LIKE '%".$id."%' or
        personal_name LIKE '%".$personalname."%' or
        surname LIKE '%".$surname."%' or
        initials LIKE '%".$initials."%' or
        email LIKE '%".$email."%' or
        telephone LIKE '%".$telephone."%' or
        adegree LIKE '%".$adegree."%'";*/

if($adegree != 0){

    $sql="SELECT * from personal  WHERE  
        id LIKE '%".$id."%' and 
        personal_name LIKE '%%' and
        surname LIKE '%".$surname."%' and 
        initials LIKE '%".$initials."%' and
        email LIKE '%".$email."%' and
        telephone LIKE '%".$telephone."%' and
        adegree LIKE '%".$adegree."%'";
}
else{
$sql="SELECT * from personal  WHERE  
        id LIKE '%".$id."%' and 
        personal_name LIKE '%%' and
        surname LIKE '%".$surname."%' and   
        initials LIKE '%".$initials."%' and
        email LIKE '%".$email."%' and
        telephone LIKE '%".$telephone."%' ";


}


$consulta= mysqli_query($conexio,$sql);

$resultat=mysqli_fetch_assoc($consulta);

if($consulta){

    if(mysqli_num_rows($consulta) >0){
        if(!empty($resultat)){


    echo "<table id='reultat' class='taula'>";
                        echo"<thead>";
                            echo "<tr>";
                                echo"<th>id</th>";
                                echo"<th>Personal name</th>";
                                echo"<th>Surname</th>";
                                echo"<th>Initials</th>";
                                echo"<th>Email</th>"; 
                                echo"<th>Telephone</th>"; 
                                echo"<th>Adegree</th>"; 
                                echo"</tr>";
                                echo"</thead>";
                                echo"<tbody";


                while($fila=mysqli_fetch_assoc($consulta)){
                            echo"<tr>";
                            echo "<td>".$fila['id']."</td>";
                            echo "<td>".$fila['personal_name']."</td>";
                            echo "<td>".$fila['surname']."</td>";
                            echo "<td>".$fila['initials']."</td>";
                            echo "<td>".$fila['email']."</td>";
                            echo "<td>".$fila['telephone']."</td>";
                            echo "<td>".$fila['adegree']."</td>";
                            echo"</tr>";
                                    }           
                                echo "</tbody>";
                                echo "<table>";

                            liberar($consulta);
                            desconectar_bd($conexio);


    }
        else{

            echo"<table id='resultat'class='noResults' align='center'>";
                echo"<tr>";
                    echo"<td>No results!</td>";
                echo"</tr>";
            echo"</table>";
            }
  }
    else{
     echo"<table id='resultat'class='noResults' align='center'>";
                echo"<tr>";
                    echo"<td>No results!</td>";
                echo"</tr>";
            echo"</table>";
    }
}
    else{

        echo"<table id='resultat'class='noResults' align='center'>";
                echo"<tr>";
                    echo"<td>Query error!</td>";
                echo"</tr>";
            echo"</table>";
    }
}
else{
    echo"no post";
}

?>

1 个答案:

答案 0 :(得分:0)

我最近使用LIKE功能为我的某个网站添加了搜索功能。

当我通过GET提交我的搜索表单时,我会根据与表单一起传递的变量构建数据库查询字符串。

                if(strcmp($_GET['SSeries'],'') != 0) {
                    $searchString .= "Series LIKE '%".$_GET['SSeries']."%' AND ";
                    $uFriend .= "Series CONTAINS '".$_GET['SSeries']."' AND ";
                }
                if(strcmp($_GET['SModel'],'') != 0) {
                    $searchString .= "Model LIKE '%".$_GET['SModel']."%' AND ";
                    $uFriend .= "Model CONTAINS '".$_GET['SModel']."' AND ";
                }
                if(strcmp($_GET['SSerial'],'') != 0) {
                    $searchString .= "Serial LIKE '%".$_GET['SSerial']."%' AND ";
                    $uFriend .= "Serial CONTAINS '".$_GET['SSerial']."' AND ";
                }
                $_SESSION['searchString'] = $searchString;

然后在最后,声明一个将它们连接在一起的变量。

然后,我只是在搜索字符串中使用该变量,如下所示:

               if(empty($_SESSION['searchString'])) {
                    $sql = "SELECT * from identification;";
                    $sqluFriend = "Search ALL";
                } else {
                    $sql = "SELECT * from identification WHERE ".substr($_SESSION['searchString'], 0, -5).";";
                    $sqluFriend = "Search ".substr($_SESSION['uFriend'], 0, -5)."";
                }

如果搜索字符串为空,我创建一个没有where子句的查询。 另外,请注意使用substr()方法,从搜索字符串中删除最后5个符号(基本上字符串不会以AND结尾,因为这会导致查询出现问题。)此外,您可以忽略$sqluFriend变量,我使用它们来显示查询的用户友好版本。

基本上,如上所示,我根据是否发布了GET变量来构建搜索字符串,这使它成为动态搜索查询。

另一件事是你应该使用if语句来包装你的$ searchString构建器,以检查是否有任何数据被发布,以避免错误/返回错误代码等。以下是我的方法:

                   if((isSet($_GET['SSSeries'])) 
                    || (isSet($_GET['SSModel'])) 
                    || (isSet($_GET['SSSerial']))) {

您当然可以轻松扩展它以便轻松满足您的需求。

每次更改输入时,我所做的就是将表单连接到ajax请求,这样当有人输入任何内容时,它会自动重新加载包含结果的表格。

希望我能提供帮助。