PHP PDO LIKE:与通配符结合时转义%字符 [英] PHP PDO LIKE : escaping the % character when combining with wildcard

查看:154
本文介绍了PHP PDO LIKE:与通配符结合时转义%字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$percent = ‘%’;
$st=$db->prepare("SELECT * FROM x WHERE y LIKE ?");
$st=$st->execute(array(‘%’.$percent.’%’)); /*I want to get all records with the string % included like 5% etc.*/

上面的示例将不正确匹配,而是匹配表x中的所有记录.为了使其正常工作,我显然需要设置$ percent ='\%'.

The above example will not match correctly, instead matching all records in table x. In order for this to work correctly, I apparently need to set $percent='\%'.

在这里,我对准备好的语句背后的概念感到困惑.我认为准备好的语句的全部意义在于,值本身($ percent)将被简单地解释为字符串,而不是特殊的通配符.我将不胜感激.

This is where I am left confused about the concept behind prepared statements. I thought the whole point of prepared statements was that the value itself( $percent) would simply be interpreted as a string instead of a special wildcard character. I would appreciate any feedback.

预先感谢

推荐答案

在PDO标记(信息)中,您将找到在参数中使用通配符的正确过程.

In the PDO tag (info) you will find the correct procedure for using wildcards in parameters.

然后您可以在参数中转义%.

Then you can escape % in the parameter.

$percent = '%\%%';//Escape % within % wildcards
.......
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
.........
$st=$db->prepare("SELECT * FROM x WHERE y LIKE ?");
$st=$st->execute(array($percent’));

这篇关于PHP PDO LIKE:与通配符结合时转义%字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆