2012-07-09 62 views
1

我有以下代码,但此代码仅适用于数据库中的精确匹配,我想要的是例如您要搜索“约翰琼斯”,如果您键入只是joh他姓名和其他同名用户将出现在下拉列表中,以及姓氏名称。 我尝试了一些与'LIKE'功能,但我似乎无法弄清楚。 谢谢!包括下拉搜索与可能的搜索名称

<?php 
    include ('core/init.php'); 

    logged_in_redirect(); 

    if(empty($_POST['friend_search'])) { 
     header('Location: friends.php'); 
    } 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 

     <title> My Friends</title> 
     <link href='css.css' rel='stylesheet' type='text/css'/> 
    </head> 
    <body class='home-body'> 
     <?php include_once("core/analyticstrackingcode.php") ?> 
     <div class='whitebg'> 
      <?php include('core/navbar.php'); ?> 
      <?php 
       $friend = $_POST['friend_search']; 
       $found=0; 
       $search = mysql_query("SELECT * FROM users"); 
       while($row = mysql_fetch_array($search)) { 
        $possible_match_name = $row['first_name']; 
        $possible_match_id = $row['id']; 
        $possible_match_email = $row['email']; 
        $possible_match_profilepic = $row['profilepic']; 
        $this_users_email = $_SESSION['email']; 
        if($possible_match_email!=$this_users_email) { 
         if($friend==$possible_match_email) { 
          echo " 
           <html> 
            <div class='friendsearchentries'> 
             <img src='$possible_match_profilepic' class='friend-match-prof-pic' /> 
             <a href='confirm_send.php?id=$possible_match_id' class='friend-match-link' ><center>".$possible_match_name."</center></a> 
            </div> 
           </html>"; 
           $found=1; 
         } 
        } 
       } 
       if($found==0) { 
        echo "Sorry, we found no matches."; 
       } 
      ?> 
     </div> 
    </body> 
</html> 

回答

0

如果您使用的MyISAM或InnoDB的引擎与MySQL,你可以使用一些内置的全文检索功能。

你就可以做的东西,如:

"SELECT * FROM users WHERE MATCH (first_name, email) AGAINST ('john jones' IN BOOLEAN MODE)" 

这是使用PHP API和Ajax构建搜索作为你型功能,例如真正有用的。

肯定有关于这些的阅读,你可能需要做一些与你的数据库工作,以确定它是否运行MyISAM或InnoDB,并可能有点配置。

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

http://dev.mysql.com/doc/refman/5.6/en/fulltext-boolean.html