本文介绍了PHP mysql 查询判断和不回显重复部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做一个 php 搜索查询.首先,我放一个句子并分解每个单词获取$name
,然后我放$name
进行查询以匹配我数据库中存在的所有名称.然后回显部分 $row['name'][$i]
有一些单词重复.
I want to make a php search query. First, I put a sentence and explode every word get $name
,Then I put $name
make a query to match all the name which is exist in my database. Then echo part $row['name'][$i]
has some word repeat.
例如:句子是:Marc Gasol is the Brother of Pau Gasol
和Gasol
在我的数据库中,所以匹配词Gasol
珍珠2次.如何echo $row['name'][$i];
并且只得到一个Gasol
?谢谢,
for example: the sentence is :Marc Gasol is the brother of Pau Gasol
and Gasol
in my database, so the match words Gasol
apearl 2 times. how to echo $row['name'][$i];
and get only one Gasol
? thanks,
$query = mysql_query("SELECT * FROM books WHERE name like '$name[0]' OR name like '$name[1]' OR name like '$name[2]' OR name like '$name[3]' ");
$i = 0;
while($row = mysql_fetch_array($query)) {
echo $row['name'][$i];
$i++;
}
推荐答案
$sentence = "Marc Gasol is the brother of Pau Gasol";
$words = preg_split("/\s+/", $sentence);
//this will uniqueify your value set
$uniqueWords = array_keys(array_flip($words));
foreach($uniqueWords as $word){
$parts[] = "name like '%".mysql_real_escape_string($word)."%'";
}
$where = implode(" OR ", $parts);
$query = mysql_query("SELECT * FROM books WHERE $where ");
$i = 0;
while($row = mysql_fetch_array($query)) {
echo $row['name'][$i];
$i++;
}
这篇关于PHP mysql 查询判断和不回显重复部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!