简体   繁体   English

使用PHP,mySQL和PDO进行LIKE查询

[英]LIKE-Query with PHP, mySQL and PDO

I want to match \\' in my a column in a table in a MySQL Database, because these are entries where the data wasn't properly escaped. 我想在MySQL数据库的表中的一列中匹配\\' ,因为这些是未正确转义数据的条目。

I use PHP with PDO, this is the relevant code: 我将PHP与PDO一起使用,这是相关代码:

$stmt = $db->prepare("SELECT * FROM table WHERE title LIKE :title");
$stmt->bindValue(':title',"%\\'%",PDO::PARAM_STR);

Problem is, this matches titles with \\' as well with a single '. 问题是,这会将标题与\\'以及单个'匹配。 I tried various combinations of \\\\\\\\\\' etc., but nothing really worked to just match \\' , not the single ' . 我尝试了\\\\\\\\\\'等的各种组合,但是仅匹配\\'而不是单个'并没有真正起作用。

What am I doing wrong? 我究竟做错了什么?

As it turns out, you actually need to do this: 事实证明,您实际上需要执行以下操作:

$stmt->bindValue(':title',"%\\\\\\'%",PDO::PARAM_STR);

That is 6 backslashes and one apostrophe which then matches one backslash and one apostrophe. 即6个反斜杠和一个撇号,然后匹配一个反斜杠和一个撇号。 Anyone got an explanation for this? 有人对此有解释吗?

PS: I don't know why just adding backslashes didn't work before, I tried that, seems like I had double quotes or something. PS:我不知道为什么以前只添加反斜杠不起作用,我尝试过,好像我有双引号或其他东西。

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

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