如何使用 LIKE 语句创建 PDO 参数化查询?

发布于 2024-07-14 01:54:45 字数 228 浏览 3 评论 0原文

这是我的尝试:

$query = $database->prepare('SELECT * FROM table WHERE column LIKE "?%"');

$query->execute(array('value'));

while ($results = $query->fetch()) 
{
    echo $results['column'];
}

Here's my attempt at it:

$query = $database->prepare('SELECT * FROM table WHERE column LIKE "?%"');

$query->execute(array('value'));

while ($results = $query->fetch()) 
{
    echo $results['column'];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

海螺姑娘 2024-07-21 01:54:45

我发帖后就明白了:

$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->execute(array('value%'));

while ($results = $query->fetch())
{
    echo $results['column'];
}

Figured it out right after I posted:

$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->execute(array('value%'));

while ($results = $query->fetch())
{
    echo $results['column'];
}
眼趣 2024-07-21 01:54:45

对于使用命名参数的用户,以下是如何将 LIKE% 部分匹配用于 MySQL 数据库

WHERE column_name LIKE CONCAT('%', :dangerousstring, '%')

其中命名参数为 :dangerousstring

换句话说,在您自己的查询中使用显式未转义的 % 符号,这些符号是分开的,并且绝对不是用户输入。

编辑:Oracle 数据库的串联语法使用串联运算符:||,因此它会简单地变为

WHERE column_name LIKE '%' || :dangerousstring || '%'

此处提到:


难度
当您希望在文本中允许使用文字 %_ 字符时出现
搜索字符串,而不将其用作通配符。

因此,在组合 like 和参数化时,还有其他需要注意的地方。

For those using named parameters, here's how to use LIKE with % partial matching for MySQL databases:

WHERE column_name LIKE CONCAT('%', :dangerousstring, '%')

where the named parameter is :dangerousstring.

In other words, use explicitly unescaped % signs in your own query that are separated and definitely not the user input.

Edit: Concatenation syntax for Oracle databases uses the concatenation operator: ||, so it'll simply become:

WHERE column_name LIKE '%' || :dangerousstring || '%'

However there are caveats as @bobince mentions here that:

The
difficulty
comes when you want to allow a literal % or _ character in the
search string, without having it act as a wildcard.

So that's something else to watch out for when combining like and parameterization.

揽清风入怀 2024-07-21 01:54:45
$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->bindValue(1, "%$value%", PDO::PARAM_STR);
$query->execute();

if (!$query->rowCount() == 0) 
{
    while ($results = $query->fetch()) 
    {
        echo $results['column'] . "<br />\n";
    }       
} 
else 
{
    echo 'Nothing found';
}
$query = $database->prepare('SELECT * FROM table WHERE column LIKE ?');
$query->bindValue(1, "%$value%", PDO::PARAM_STR);
$query->execute();

if (!$query->rowCount() == 0) 
{
    while ($results = $query->fetch()) 
    {
        echo $results['column'] . "<br />\n";
    }       
} 
else 
{
    echo 'Nothing found';
}
孤独患者 2024-07-21 01:54:45

你也可以尝试一下这个。 我面临类似的问题,但经过研究后得到了结果。

$query = $pdo_connection->prepare('SELECT * FROM table WHERE column LIKE :search');

$stmt= $pdo_connection->prepare($query);

$stmt->execute(array(':search' => '%'.$search_term.'%'));

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

print_r($result);

You can also try this one. I face similar problem but got result after research.

$query = $pdo_connection->prepare('SELECT * FROM table WHERE column LIKE :search');

$stmt= $pdo_connection->prepare($query);

$stmt->execute(array(':search' => '%'.$search_term.'%'));

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

print_r($result);
滴情不沾 2024-07-21 01:54:45

这有效:

search `table` where `column` like concat('%', :column, '%')

This works:

search `table` where `column` like concat('%', :column, '%')
单身情人 2024-07-21 01:54:45

我从 php delusions 得到这个,

$search = "%$search%";
$stmt  = $pdo->prepare("SELECT * FROM table WHERE name LIKE ?");
$stmt->execute([$search]);
$data = $stmt->fetchAll();

它对我有用,非常简单。 就像他说的,在将其发送到查询之前,您必须“先准备好完整的文字”

I got this from php delusions

$search = "%$search%";
$stmt  = $pdo->prepare("SELECT * FROM table WHERE name LIKE ?");
$stmt->execute([$search]);
$data = $stmt->fetchAll();

And it works for me, very simple. Like he says , you have to "prepare our complete literal first" before sending it to the query

白日梦 2024-07-21 01:54:45

PDO 转义“%”(可能导致 sql 注入):在查找匹配部分字符串时,使用前面的代码将给出所需的结果但是如果访问者键入字符“%”,即使数据库中没有存储任何内容,您仍然会得到结果(它可能会导致sql注入)

我尝试了很多变体,结果都相同 PDO 正在转义“%”,导致不需要的内容/不兴奋的搜索结果。

我认为值得分享,如果有人找到了与之相关的词,请分享

PDO escapes "%" (May lead to sql injection): The use of the previous code will give the desire results when looking to match partial strings BUT if a visitor types the character "%" you will still get results even if you don't have anything stored in the data base (it may lead sql injections)

I've tried a lot of variation all with the same result PDO is escaping "%" leading unwanted/unexcited search results.

I though it was worth sharing if anyone has found a word around it please share it

亚希 2024-07-21 01:54:45

我有类似的需求,但使用的是从表单中抓取的变量。 我这样做是为了使用 PHP 从 PostgreSQL 数据库获取结果:

<?php
     $player = $_POST['search'];  //variable from my search form
     $find = $sqlPDO->prepare("SELECT player FROM salaries WHERE player ILIKE ?;");
     $find->execute(['%'.$player.'%']);

     while ($row = $find->fetch()) {
         echo $row['player']."</br>";
     }
?>

“ILIKE”使搜索不区分大小写,因此搜索 cart 或 Cart 或 cARt 将返回相同的结果。

I had a similar need but was using a variable grabbed from a form. I did it like this to get results from my PostgreSQL DB, using PHP:

<?php
     $player = $_POST['search'];  //variable from my search form
     $find = $sqlPDO->prepare("SELECT player FROM salaries WHERE player ILIKE ?;");
     $find->execute(['%'.$player.'%']);

     while ($row = $find->fetch()) {
         echo $row['player']."</br>";
     }
?>

The "ILIKE" makes the search non-case sensitive, so a search for cart or Cart or cARt will all return the same results.

皓月长歌 2024-07-21 01:54:45

我可以让它工作的唯一方法是将 %$search% 放入另一个变量中。

    if(isset($_POST['submit-search'])){
         $search = $_POST['search'];
   }
    
        $query = 'SELECT * FROM posts WHERE post_title LIKE :search';
        $value ="%$search%";
        $stmt= $pdo->prepare($query);
        
        $stmt->execute(array(':search' => $value));

我不知道这是否是最好的方法,在我使用的 while 循环中:

while ($r = $stmt->fetch(PDO::FETCH_ASSOC)){

The only way I could get this to work was to put the %$search% into another variable.

    if(isset($_POST['submit-search'])){
         $search = $_POST['search'];
   }
    
        $query = 'SELECT * FROM posts WHERE post_title LIKE :search';
        $value ="%$search%";
        $stmt= $pdo->prepare($query);
        
        $stmt->execute(array(':search' => $value));

I don't know if this is the best way to do it, in the while loop I used:

while ($r = $stmt->fetch(PDO::FETCH_ASSOC)){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文