PHP / MySQL中的多字搜索 [英] Multi word search in PHP/MySQL

查看:114
本文介绍了PHP / MySQL中的多字搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$ b foreach($ explodedSearch as $ search){
$ query =SELECT *
FROM jobseeker
像'%$ search%或姓'%$ 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 = \quickLocation \>。 $位置。 < / p>
< / div>;


$ b code
$ b我也尝试了以下得到的结果,但你可以想象,我得到了每一个字我输入重复结果:

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

$ b foreach($ explodedSearch as $ search){
$ query =SELECT *
FROM jobseeker
像'%$ search%或姓'%$ 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 = \quickLocation \>。 $位置。 < / p>
< / div>;





$我几乎在

编辑:



<$ p $如果($ _ POST){
$ quickSearch = $ _POST ['quickSearch'];
$ explodedSearch = explode(,trim($ quickSearch));


$ queryArray = array();

foreach($ explodedSearch as $ search){
$ term = mysql_real_escape_string($ search);
$ queryArray [] =像%这样的名字。 $学期。 %姓氏就像%。 $学期。 %;
}

$ implodedSearch = implode('或',$ 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 = \quickLocation \>。 $位置。 < / p>
< / div>;


$ b $ / code $ / $ p

解决方案



  $ words = $ _POST ['keywords']; 
if(empty($ words)){
//在其他地方重定向!
}
$ parts = explode(,trim($ words));
$ clauses = array();
foreach($ parts as $ part){
//在我的例子中是function_description,用你想要的任何东西代替它
$ clauses [] =function_description LIKE'%。 mysql_real_escape_string($ part)。 %;
}
$ clause = implode('OR',$ clauses);
//选择你的条件并添加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 any whatever u want!
}
?>

- 现在当我试图用FULLTEXT搜索运行它时,它是这样的:
但是,您应该将表类型设置为MyISAM。

$ p $ <?php
$ words = mysql_real_escape_string($ _POST [ 'function_keywords']);
if(empty($ words)){
redirect(welcome.php?error = search_empty);
}
//如果列(结果)> 1/2(列)=> (使用NATURAL LANGUAGE=BOOLEAN)
$ sql =SELECT * FROM functions
WHERE MATCH(function_description)
AGAINST('{$ words}'IN自然语言模式);
$ 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
}
}
?>


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中的多字搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆