简体   繁体   English

PHP PDO MySQL 查询 LIKE -> 多个关键字

[英]PHP PDO MySQL query LIKE -> multiple keywords

I have a users table in MySQL and would like a search by name.我在 MySQL 中有一个用户表,想按名称搜索。 Right now I have the following code:现在我有以下代码:

<?php
$search = @$_GET['q'];
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);

$query = $con->prepare('SELECT * FROM `users` WHERE name LIKE ?');
$query->execute(array('%'.$search.'%'));

$result = $query->rowCount();
echo $result;
?>

The problem is that I want to have multiple keywords.问题是我想要多个关键字。 Let's say someone types "Here should be a nice name of a person" then it would search for "here", "should", "be" etc. and display results for every row where there words are in 'name' column.假设有人输入“这里应该是一个人的好名字”,然后它会搜索“这里”、“应该”、“是”等,并显示“姓名”列中有单词的每一行的结果。 I searched on the web and read that it is possible to do "OR name LIKE ?"我在网上搜索并读到可以做“OR name LIKE?” as many times as the keywords, but I could not really get it working and I'm not sure if it is optimized enough with ~20 words (in case they search with that many words).与关键字一样多,但我无法真正让它工作,我不确定它是否用大约 20 个词进行了足够的优化(以防他们用那么多词进行搜索)。 If it should be used, can you help me change my code so it would search for every word independently?如果应该使用它,您能帮我更改我的代码,以便它独立搜索每个单词吗?

Thank you!谢谢!

EDIT:编辑:

I was able to fix this issue by one guy who posted in this thread.我能够通过在此线程中发布的一个人解决此问题。 The following solution works for me:以下解决方案对我有用:

<?php
$search = isset($_POST['q']) ? $_POST['q'] : '';
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);

$split_words = explode(" ", $search);

if(count($split_words) > 0) {

    $sql = "SELECT * FROM `users` WHERE ";

    for($i=0 ; $i < count($split_words); $i++){
        $sql .= " name LIKE ? OR";
    }

    $sql = substr($sql , 0, -3); //Remove last 3 characters OR with space
    array_walk($split_words, "addPercentage");


    $query = $con->prepare($sql);
    $query->execute($split_words);
}

function addPercentage(&$value, $key) {
    $value = '%'.$value.'%';
}
?>

You shouldn't use @ to silence errors it is a bad practice, check if the value is set.您不应该使用 @ 来消除错误,这是一种不好的做法,请检查该值是否已设置。 The example below should work, but the results might not be all that relevant.下面的示例应该可以工作,但结果可能并不完全相关。

$search = isset($_GET['q']) ? $_GET['q'] : ''; 
$search = strtoupper($search);
$search = strip_tags($search); 
$search = trim($search);
$words = explode(' ', $search);
$words_condition = array();
$arguments = array();
foreach ($words as $word) {
    $words_condition[] = 'name LIKE ?';
    $arguments[] = '%'.$word.'%';
}

$query = $con->prepare('SELECT * FROM `users` WHERE '.implode(' OR ', $words_condition));
$query->execute($arguments);

$result = $query->rowCount();
echo $result;
$words = explode(" ", $search);

$i = 0;
while($words[i] != null)
{
    //Query where name LIKE words[i]
}

Please check the below code.请检查以下代码。

<?php
$search = isset($_GET['q']) ? $_GET['q'] : '';
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);

$split_words = explode(" ", $search);

$query = "SELECT * FROM `users`";

if(count($split_words) > 0){
 $query .= " WHERE "
 for($i=0 ; $i < $split_words; $i++){
   $query .= " name LIKE ? OR ";
 }
 $query = substr($query , 0, -3); //Remove last 3 characters OR with space


 array_walk($split_words,"addPercentage");

 $query->execute($split_words);
}else{
 $query->execute();
}

$result = $query->rowCount();
echo $result;

function addPercentage(&$value,$key)
{
  $value = "%".$value."%" ;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM