Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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中的网站搜索引擎_Php_Search - Fatal编程技术网

PHP中的网站搜索引擎

PHP中的网站搜索引擎,php,search,Php,Search,我要为我的网站创建搜索引擎。这实际上是我第一次尝试做这样的事情,所以我浏览了一下互联网和StackOverflow,但我仍然有一些问题,所以我希望你能给我一些建议 现在,考虑一下我应该搜索的表的简化版本: +----+--------+------------+ |ID | Title | Description| +----+--------+------------+ |1 | Title1 | Lorem ipsum| +----+--------+------------+ |2

我要为我的网站创建搜索引擎。这实际上是我第一次尝试做这样的事情,所以我浏览了一下互联网和StackOverflow,但我仍然有一些问题,所以我希望你能给我一些建议

现在,考虑一下我应该搜索的表的简化版本:

+----+--------+------------+
|ID  | Title  | Description|
+----+--------+------------+
|1   | Title1 | Lorem ipsum|
+----+--------+------------+
|2   | Title2 | Dolor sit  |
+----+--------+------------+
用户在搜索栏中写入的字符串:

Lorem ipsum dolor sit amet.
我的想法是:

$string = mysqli_real_escape_string($_GET['string']);
$words = explode(' ', $string);
$count_words = count($words);
for($i = 0; $i < $count_words, $i++) {
 $search_query = "SELECT * FROM 'table' WHERE 'title' = '". $words[$i] . "'";
}
那么我有两个选择

从表中选择每一行,然后检查数组$words的元素是否是字段的一部分:

$description_array = explode(' ', $description); // I get this var from the query
for($i = 0; $i < $count_words; $i++) {

 if(in_array($words[$i]), $description_array) {
   // Something here
  }

}
$description\u数组=分解(“”,$description);//我从查询中得到这个var
对于($i=0;$i<$count_words;$i++){
if(在数组中($words[$i]),$description\u数组){
//这里有东西
}
}
或者,更好的方法是:

$string = mysqli_real_escape_string($_GET['string']);
$words = explode(' ', $string);
$count_words = count($words);
for($i = 0; $i < $count_words, $i++) {
 $search_query = "SELECT * FROM 'table' WHERE 'title' = '". $words[$i] . "'";
}
for($i=0;$i<$count\u单词,$i++){
$search_query=“从‘表’中选择*,其中‘标题’=”。$words[$i]。”;
}
这就是:最好先选择所有内容,然后检查数据,还是只选择符合字符串的行?或者,有没有一种标准的方式来编写这个脚本

提前感谢。

您可以使用:

$wordsToSearch = array();

$string = mysqli_real_escape_string($_GET['string']);
$words = explode(' ', $string);

foreach($word as $val){
  $w = "`title` LIKE '%".$val."%' OR `description` LIKE '%".$val."%'";
  $wordsToSearch[] = $w;
}

$search_query = "SELECT * FROM 'table' WHERE ".implode("OR",$wordsToSearch);

我希望这对您有用。

请尝试以下内容:

$string = mysqli_real_escape_string(trim($_GET['string']));
$words = explode(' ', $string);

//Check if the user wrote more than a single word
if (count($words)>1) {
    //Then you can create only one query and then you can fetch the result of that
    //Implode the words
    $search_query = "SELECT * FROM table WHERE title='".implode("' OR title='", $words)."'";
}
else { //If the user wrote only one word
    $search_query = "SELECT * FROM table WHERE title='".$string."'";
}
echo $search_query;

你可以试试这个

你应该看看MySQL全文搜索——也许自然语言搜索最合适。哇,谢谢,这太棒了。