Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在MySQL中搜索多个单词_Php_Mysql - Fatal编程技术网

Php 在MySQL中搜索多个单词

Php 在MySQL中搜索多个单词,php,mysql,Php,Mysql,我使用HTML表单允许用户在数据库表中查找条目: <form action="controller.php" method="get"> <input type="text" name="word" id="word"> 这很好,除非用户在搜索框中键入多个单词。我如何修改它,当用户在搜索框中键入多个单词时,它将返回在其关键字列中包含用户单词的所有行 i、 例如,用户搜索“apples-oranges”,查询将返回关键字字段中包含“apples”或“orang

我使用HTML表单允许用户在数据库表中查找条目:

   <form action="controller.php" method="get">
   <input type="text" name="word" id="word">
这很好,除非用户在搜索框中键入多个单词。我如何修改它,当用户在搜索框中键入多个单词时,它将返回在其关键字列中包含用户单词的所有行


i、 例如,用户搜索“apples-oranges”,查询将返回关键字字段中包含“apples”或“oranges”的所有行

LIKE
命令适用于一个单词字符串。要在数据库中进行高级搜索,请使用
全文
搜索


您可以使用正则表达式:

$words = explode(' ', $_GET['word']);
$regex = implode('|', $words);

$query = "SELECT * FROM table WHERE table.keywords REGEXP '{$regex}'";
你可以试试-

$words = explode(' ', $word);
$query = "SELECT * FROM table WHERE table.keywords IN (".implode(',', $words).")";


如果需要,可以添加检查。

与SQL中无法搜索单个名称一样,请尝试根据空格(如Roads Road)拆分名称,并在最终查询中附加到字符串(即(
'%Roads%')
)或(
%Road%

SELECT id, gps, street_name 
FROM streets 
WHERE street_name LIKE ('%Heavens%') OR (%Road%) 

e、 g如果更多的术语
像(%1term%)或(%2term%)或(%3term%)或(%4term%)
正则表达式为我做这件事

$str = $_GET['words']; 
$commonwords = 'a,an,and,I,it,is,do,does,for,from,go,how,the,this,are';// you dont want to search for these common words

$commonwords = explode(",", $commonwords);

$str = explode(" ", $str);

foreach($str as $value){
  if(!in_array($value, $commonwords)){ // remove the common words from search

    $query[] = $value;
  }
}   

$query = implode(" ", $query);// generate coma separated values 

$str2 =(explode(" ",$query));// convert the values to an array
$Words = implode('|',array_values($str2)).'';// generate values to be searched and make regex

"SELECT * FROM table_name WHERE `keywords` REGEXP '$Words'"

对于我来说,当用户搜索多个产品时,每个产品由一个空格分隔开。用户不必键入完整的单词

我使用此代码,工作正常

    $regcond = trim($_GET['word']);
    $regcond = preg_replace('!\s+!', ' ', $regcond); //change how many space beetwen word to one space
    $regcond = trim($regcond);
    $regcond = str_replace(" ",".+",$regcond); // change all space to .+ for search with regex
    $final_cond = 'SELECT * FROM table WHERE table.keywords REGEXP "' . $regcond . '"';

这是我的PHP解决方案,其中搜索字符串包含多个关键字,可以是任意顺序,可以用多个空格分隔,可以有重复,必须根据找到的关键字数量进行排序,并且是sql注入安全的。假设表被称为topicd,ID为TopicID,TopicText列中的文本和keywords列中的关键字,并且您的mySQL连接是$dbConn

//convert search string to lowercase - will do case insensitive search later
$words = trim(strtolower($_GET['k']));
//remove punctuation and wildcards
$words = str_replace(array('*', '?', '-', '.', '/', '(', ')', '+', '&'), '', $words);
//turn the string into an array of unique words
$words = array_unique(explode(' ', $words));
//strip the words we are not interested in
$stopWords = array('', '*',  '?', 'a', 'about', 'an', 'and','are', 'as', 'at', 'be', 'by', 'for', 'from', 'how', 'in', 'is', 'it', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'what', 'when', 'where', 'who', 'will', 'with');
foreach($stopWords as $word) {
    if (($key = array_search($word, $words)) !== false) unset($words[$key]);
}
//build SQL statement
$sql =  "select TopicText, Score from (select TopicID,  0";
foreach($words as $word) {
    if($word>'') $sql .= "+if(Keywords regexp '" . $dbConn->real_escape_string($word) . "',1,0)";
}
$sql .=  " as Score from Topics) T join Topics TT on TT.TopicID = T.TopicID where T.Score>0 order by Score desc";

如果没有包含关键字的列,可以使用TopicText列作为上面的regexp目标。

分解搜索词并循环遍历数组结果,在数组结果中创建多个LIKE语句并附加到查询中。如果使用全文搜索会怎么样?这可能会以类似谷歌的顺序(按相关性)对结果进行排序。键入的字数是动态的,而您的代码则不是。如果它包含两个以上的字数?不幸的是,这根本不是一个灵活的解决方案:(幸运的是,有一种灵活的方法可以循环单词并构造查询。@Daan、BOSE中士、jeromegamez、bksi。感谢您的反馈,请检查编辑后的答案。添加全文索引无助于操作。通配符在哪里?:)需要精确匹配。是的。对于这种情况,它需要精确匹配。也可以相应地生成查询。@Daan更新了答案。现在通配符可以工作了。Thanx sgt,工作得很好。只是出于兴趣,什么是通配符?
$str = $_GET['words']; 
$commonwords = 'a,an,and,I,it,is,do,does,for,from,go,how,the,this,are';// you dont want to search for these common words

$commonwords = explode(",", $commonwords);

$str = explode(" ", $str);

foreach($str as $value){
  if(!in_array($value, $commonwords)){ // remove the common words from search

    $query[] = $value;
  }
}   

$query = implode(" ", $query);// generate coma separated values 

$str2 =(explode(" ",$query));// convert the values to an array
$Words = implode('|',array_values($str2)).'';// generate values to be searched and make regex

"SELECT * FROM table_name WHERE `keywords` REGEXP '$Words'"
    $regcond = trim($_GET['word']);
    $regcond = preg_replace('!\s+!', ' ', $regcond); //change how many space beetwen word to one space
    $regcond = trim($regcond);
    $regcond = str_replace(" ",".+",$regcond); // change all space to .+ for search with regex
    $final_cond = 'SELECT * FROM table WHERE table.keywords REGEXP "' . $regcond . '"';
$words = explode(" ",$string);
$string = array_map(function(&$word){
    return "+".$word."*";
},$words);

$term = implode(" ",$string);
$query = "SELECT * FROM table_name WHERE MATCH(column_name) AGAINST('{$term}' IN BOOLEAN MODE)";
//convert search string to lowercase - will do case insensitive search later
$words = trim(strtolower($_GET['k']));
//remove punctuation and wildcards
$words = str_replace(array('*', '?', '-', '.', '/', '(', ')', '+', '&'), '', $words);
//turn the string into an array of unique words
$words = array_unique(explode(' ', $words));
//strip the words we are not interested in
$stopWords = array('', '*',  '?', 'a', 'about', 'an', 'and','are', 'as', 'at', 'be', 'by', 'for', 'from', 'how', 'in', 'is', 'it', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'what', 'when', 'where', 'who', 'will', 'with');
foreach($stopWords as $word) {
    if (($key = array_search($word, $words)) !== false) unset($words[$key]);
}
//build SQL statement
$sql =  "select TopicText, Score from (select TopicID,  0";
foreach($words as $word) {
    if($word>'') $sql .= "+if(Keywords regexp '" . $dbConn->real_escape_string($word) . "',1,0)";
}
$sql .=  " as Score from Topics) T join Topics TT on TT.TopicID = T.TopicID where T.Score>0 order by Score desc";