PHP/MySQL 中的多词搜索

Multi word search in PHP/MySQL(PHP/MySQL 中的多词搜索)
本文介绍了PHP/MySQL 中的多词搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在努力创建一个搜索多个词的搜索.我的第一次尝试没有任何结果,如下:

 require_once('database_conn.php');如果($_POST){$explodedSearch = 爆炸 (" ", $_POST['quickSearch']);foreach($explodedSearch 作为 $search){$query = "选择 *来自求职者WHERE 前名如%$search%"或姓氏如%$search%"按用户 ID 排序限制 5";$result = mysql_query($query);}while($userData=mysql_fetch_array($result)){$forename=$userData['forename'];$surname=$userData['surname'];$profPic=$userData['profilePicture'];$location=$userData['location'];echo "

<img 类="quickImage" src="" .$profPic."" 宽度="45" 高度="45"/><p class="quickName">" . $forename . " " . $surname . "</p><p class="quickLocation">" . $location . "</p>

";}}

我也尝试了以下方法,结果得到了结果,但正如您想象的那样,我输入的每个单词都得到了重复的结果:

if($_POST){$explodedSearch = 爆炸 (" ", $_POST['quickSearch']);foreach($explodedSearch 作为 $search){$query = "选择 *来自求职者WHERE 前名如%$search%"或姓氏如%$search%"按用户 ID 排序限制 5";$result .= mysql_query($query);while($userData=mysql_fetch_array($result)){$forename=$userData['forename'];$surname=$userData['surname'];$profPic=$userData['profilePicture'];$location=$userData['location'];echo "

<img 类="quickImage" src="" .$profPic."" 宽度="45" 高度="45"/><p class="quickName">" . $forename . " " . $surname . "</p><p class="quickLocation">" . $location . "</p>

";}}}

我非常不知道如何进行此操作,任何帮助将不胜感激.

if($_POST){$quickSearch = $_POST['quickSearch'];$explodedSearch = 爆炸(",修剪($quickSearch));$queryArray = array();foreach($explodedSearch 作为 $search){$term = mysql_real_escape_string($search);$queryArray[] = "forename like '%" .$term ."%' 姓氏如 '%" .$term ."%'";}$implodedSearch = implode(' or ', $queryArray);$query="选择 *来自求职者WHERE ($implodedSearch)按用户 ID 排序限制 5";$result = mysql_query($query);while($userData=mysql_fetch_array($result, MYSQL_ASSOC)){$forename=$userData['forename'];$surname=$userData['surname'];$profPic=$userData['profilePicture'];$location=$userData['location'];echo "

<img 类="quickImage" src="" .$profPic."" 宽度="45" 高度="45"/><p class="quickName">" . $forename . " " . $surname . "</p><p class="quickLocation">" . $location . "</p>

";}}

解决方案

我一直在研究同一主题(用关键字搜索)一段时间,这是我是如何做到的:

$words = $_POST['keywords'];如果(空($words)){//重定向到其他地方!}$parts = expand(" ",trim($words));$子句=数组();foreach ($parts 作为 $part){//function_description 在我的情况下,用你表中你想要的任何东西替换它$clauses[]="function_description LIKE '%" .mysql_real_escape_string($part) ."%'";}$clause=implode(' OR ' ,$clauses);//选择您的条件并添加 "AND ($clauses)" .$sql="选择 *FROM 函数在哪里用户名='{$用户名}'AND ($子句) ";$results=mysql_query($sql,$connection);如果(!$结果){重定向(错误/error_db.html");}否则如果($结果){$rows = array();<?phpwhile($rows = mysql_fetch_array($results, MYSQL_ASSOC)){//回显你想要的任何东西!}?>

-- 现在这是我尝试使用 FULLTEXT 搜索运行它时的样子:但是您应该将表类型设置为MyISAM"

 1/2(列)=>它不会返回任何东西!(使用自然语言"=布尔值")$sql="SELECT * FROM 函数哪里匹配(function_description)反对 ('{$words}' 自然语言模式)";$results=mysql_query($sql,$connection);如果(!$结果){重定向(错误/error_db.html");}否则如果($结果){$rows = array();while($rows = mysql_fetch_array($results, MYSQL_ASSOC)){//回声}}?>

I'm struggling to create a search that searches for multiple words. My first attempt yielded no results whatsoever and is as follows:

  require_once('database_conn.php');
  if($_POST){
  $explodedSearch = explode (" ", $_POST['quickSearch']);


  foreach($explodedSearch as $search){
  $query = "SELECT * 
            FROM jobseeker 
            WHERE forename like '%$search%' or surname like '%$search%' 
            ORDER BY userID 
            LIMIT 5";
  $result = mysql_query($query);
}

while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class="result">
    <img class="quickImage" src="" . $profPic. "" width="45" height="45"/>
    <p class="quickName">" . $forename . " " . $surname . "</p>
    <p class="quickLocation"> " . $location . "</p>
    </div>";

}
}  

I also tried the following, which yielded results, but as you can imagine, I was getting duplicate results for every word I entered:

if($_POST){
$explodedSearch = explode (" ", $_POST['quickSearch']);


foreach($explodedSearch as $search){
$query = "SELECT * 
          FROM jobseeker 
          WHERE forename like '%$search%' or surname like '%$search%' 
          ORDER BY userID 
          LIMIT 5";
$result .= mysql_query($query);


while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class="result">
    <img class="quickImage" src="" . $profPic. "" width="45" height="45"/>
    <p class="quickName">" . $forename . " " . $surname . "</p>
    <p class="quickLocation"> " . $location . "</p>
    </div>";
}
}
}

I'm pretty much at a loss as to how to proceed with this, any help would be greatly appreciated.

EDIT:

if($_POST){
$quickSearch = $_POST['quickSearch'];
$explodedSearch = explode (" ", trim($quickSearch));


$queryArray = array();

foreach($explodedSearch as $search){
$term = mysql_real_escape_string($search);
$queryArray[] = "forename like '%" . $term .  "%' surname like '%" . $term . "%'";
}

$implodedSearch = implode(' or ', $queryArray);

$query="SELECT *
        FROM jobseeker
        WHERE ($implodedSearch)
        ORDER BY userID
        LIMIT 5";

$result = mysql_query($query);

while($userData=mysql_fetch_array($result, MYSQL_ASSOC)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];


    echo "<div class="result">
    <img class="quickImage" src="" . $profPic. "" width="45" height="45"/>
    <p class="quickName">" . $forename . " " . $surname . "</p>
    <p class="quickLocation"> " . $location . "</p>
    </div>";

}
}

解决方案

I've been working on the same subject (search with keywords) for a while and this how i did it :

$words = $_POST['keywords'];
if(empty($words)){
    //redirect somewhere else!
}
$parts = explode(" ",trim($words));
$clauses=array();
foreach ($parts as $part){
    //function_description in my case ,  replace it with whatever u want in ur table
    $clauses[]="function_description LIKE '%" . mysql_real_escape_string($part) . "%'";
}
$clause=implode(' OR ' ,$clauses);
//select your condition and add "AND ($clauses)" .
$sql="SELECT * 
      FROM functions 
      WHERE
      user_name='{$user_name}'
      AND ($clause) ";
$results=mysql_query($sql,$connection);
 if(!$results){
    redirect("errors/error_db.html");
 }
 else if($results){
 $rows = array();
<?php 
 while($rows = mysql_fetch_array($results, MYSQL_ASSOC))
{
   // echo whatever u want !
}
?>

-- Now this is how it look when i tried to run it with FULLTEXT search : But you should set the table type as "MyISAM"

<?php
$words = mysql_real_escape_string($_POST['function_keywords']);
if(empty($words)){
    redirect("welcome.php?error=search_empty");
}
//if the columns(results)>1/2(columns) => it will return nothing!(use "NATURAL LANGUAGE"="BOOLEAN")
$sql="SELECT * FROM functions
     WHERE MATCH (function_description)
     AGAINST ('{$words}' IN NATURAL LANGUAGE MODE)";
$results=mysql_query($sql,$connection);
 if(!$results){
    redirect("errors/error_db.html");
 }
 else if($results){
$rows = array();
while($rows = mysql_fetch_array($results, MYSQL_ASSOC))
{
     // echo 
}
}
?>

这篇关于PHP/MySQL 中的多词搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Difference between mt_rand() and rand()(mt_rand() 和 rand() 的区别)
Coalesce function for PHP?(PHP的合并函数?)
Fastest way to convert string to integer in PHP(在 PHP 中将字符串转换为整数的最快方法)
2 Column Mysql Date Range Search in PHP(PHP中的2列Mysql日期范围搜索)
mysql match against ~ example(mysql 匹配 ~ 示例)
How-to: Ranking Search Results(操作方法:对搜索结果进行排名)