[英]search for multiple keywords with php and mysql (where X like)
I have a code that dynamically search for data in the database using ajax but I can search for only 1 keyword in a time. 我有一个代码,使用ajax动态搜索数据库中的数据,但我一次只能搜索1个关键字。 I would like to modify it so I can search for multiple keywords. 我想修改它,以便我可以搜索多个关键字。 Now, if I type 2 keywords separated by a space and in the database, the data is not separated by a space, there will be no result. 现在,如果我输入由空格分隔的2个关键字,并且在数据库中,数据没有用空格分隔,就没有结果。 If in the database the data is: 如果在数据库中数据是:
'playstation3' or 'play cool station3' 'playstation3'或'play cool station3'
and I search for: 我搜索:
play station 玩站
there would be no results. 没有结果。 I would like to know if it possible to modify my code so I can search 2 or more keywords or words separated by a space or another word or a DOT or an underscore or a (-) or a (+) or a (%) or (anything else lol). 我想知道是否可以修改我的代码,这样我就可以搜索2个或更多的关键字或用空格或其他单词或DOT或下划线或( - )或a(+)或a(%)分隔的单词或(其他任何大声笑)。
I know that I should use pdo or mysqli but i'm using this for testing only! 我知道我应该使用pdo或mysqli,但我只是用它进行测试!
$queried = $_POST['query'];
$search = mysql_query("SELECT * FROM links WHERE name LIKE '%$queried%'");
while($searche = mysql_fetch_array($search)){
echo "".$searche['link']."</br>".$searche['name']."</br>".$searche['size']."</br>".$searche['category']."<hr></br></br>";
}
To dynamically search all keywords, you can use the explode function to seperate all keywords; 要动态搜索所有关键字,您可以使用explode功能分隔所有关键字;
$queried = mysql_real_escape_string($_POST['query']); // always escape
$keys = explode(" ",$queried);
$sql = "SELECT * FROM links WHERE name LIKE '%$queried%' ";
foreach($keys as $k){
$sql .= " OR name LIKE '%$k%' ";
}
$result = mysql_query($sql);
Note 1: Always escape user input before using it in your query. 注意1:在查询中使用之前,请务必转义用户输入。
Note 2: mysql_* functions are deprecated, use Mysqli or PDO as an alternative 注2:不推荐使用mysql_ *函数,使用Mysqli或PDO作为替代
Update 2018 - Note 3: Don't forget to check the length of the $queried
variable and set a limit. 更新2018年 - 注3:不要忘记检查$queried
变量的长度并设置限制。 Otherwise the user can input a vary large string and crash your database. 否则,用户可以输入变大的字符串并使数据库崩溃。
Don't use mysql_*
functions even for testing anymore. 不要再使用mysql_*
函数进行测试了。 They are no easier than mysqli
, just in case you think easy to test here then move. 它们并不比mysqli
容易,只是如果你认为在这里容易测试然后移动。
However, you could split your input on a ,
and try this 但是,您可以将输入拆分为a ,
并尝试此操作
<?php
$queried="abc,test, testing";
$values=explode(',',$queried);
$sql="SELECT * FROM links WHERE";
$i=0;
foreach($values as $v)
{
$v=trim($v);
if($i==0)
{
$sql.=" name LIKE '%$v%'";
}
else
{
$sql.=" OR name LIKE '%$v%'";
}
$i++;
}
$search = mysql_query($sql);
while($searche = mysql_fetch_array($search))
{
echo "".$searche['link']."</br>".$searche['name']."</br>".$searche['size']."</br>".$searche['category']."<hr></br></br>";
}
?>
This is a MySQL PDO version of @Hanky Panky's answer 这是@Hanky Panky的答案的MySQL PDO版本
$queried="abc,test, testing";
$values=explode(',',$queried);
$sql="SELECT * FROM citations_new WHERE";
$i=0;
foreach($values as $v){
$v=trim($v);
if($i==0) {
$sql.=" name LIKE '%$v%'";
}
else {
$sql.=" OR name LIKE '%$v%'";
}
$i++;
}
$sth = $this->db->prepare($sql);
$sth->execute();
$rows = $sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()){
//enter code here
}
Just add an OR clause. 只需添加一个OR子句。
Eg 例如
SELECT * FROM links WHERE name LIKE '%$keyword1%' OR name LIKE '%$keyword2%'
But I strongly recommend you using a parametrized query or other safe library to avoid Sql Injection Attacks 但我强烈建议您使用参数化查询或其他安全库来避免Sql注入攻击
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.