如何在 Zend Framework 中转义复杂的 sql?
我有以下 sql(实际问题的简化):
SELECT *
FROM t
WHERE myname LIKE '%{$input}%';
如何转义 $input?
我无法使用 quoteInto (除非我错过了什么)。
正如
$sql=$DB->quoteInto("SELECT *
FROM t
WHERE myname LIKE '%?%'",$input);
威尔给我
SELECT *
FROM t
WHERE myname LIKE '%'my input'%';
和
$sql=$DB->quoteInto("SELECT *
FROM t
WHERE myname LIKE ?",'%'.$input.'%');
威尔给我一些话:
SELECT *
FROM t
WHERE myname LIKE '\%my input\%';
I have the following sql (a simplification of the real problem):
SELECT *
FROM t
WHERE myname LIKE '%{$input}%';
How do I escape the $input?
I can't use the quoteInto (unless I miss something).
As
$sql=$DB->quoteInto("SELECT *
FROM t
WHERE myname LIKE '%?%'",$input);
Will give me
SELECT *
FROM t
WHERE myname LIKE '%'my input'%';
and
$sql=$DB->quoteInto("SELECT *
FROM t
WHERE myname LIKE ?",'%'.$input.'%');
Will give me something on the lines:
SELECT *
FROM t
WHERE myname LIKE '\%my input\%';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最后一个选项对我来说效果很好,我还没有经历过它转义“%”。 因此
$db->quote('%'.$_GET['query'].'%')
输出%queryvalue%
The last option is works out well for me i've not experienced it escaping '%'. So
$db->quote('%'.$_GET['query'].'%')
outputs%queryvalue%
解决方案非常简单。 Zend_Db 有一个表达式类可以帮助您解决它。
The solution is really simple. Zend_Db has een Expression class that helps you work arround it.
您可以在 SQL 级别进行 $input 的串联:
不幸的是,当您希望 $input 能够包含文字“%”或“_”字符时,这不可用。 要解决这个问题,请指定一个显式的 LIKE-ESCAPE 字符并自行转义它们:(
它可以是任何字符,不一定是“=”。这也解决了 MySQL 中未指定时 ESCAPE 默认为“\”的错误。)
不幸的是,SQL Server 也将“[”字符视为特殊字符,以执行类似正则表达式的字符组。 因此,如果您的数据库是 SQL Server,则必须在 preg_replace 的组中包含“[”。 不幸的是,在其他不需要转义的 DBMS 上,转义 '[' 并不是有效的 ANSL SQL。
You can do the concatenation of $input at the SQL level:
Unfortunately this isn't usable when you want $input to be able to contain literal ‘%’ or ‘_’ characters. To get round this, specify an explicit LIKE-ESCAPE character and escape them yourself:
(It can be any character, not necessarily '='. This also works around a bug where ESCAPE defaults to ‘\’ when not specified in MySQL.)
Unfortunately SQL Server also takes the ‘[’ character as special, to do a regexp-like character group. So if your DB is SQL Server you have to include ‘[’ in the group in preg_replace. Unfortunately it is not valid ANSL SQL to escape ‘[’ on other DBMSs where it doesn't need to be escaped.
很简单:
问题是什么?
:)
It is very simple:
What is the prolem?
:)
问题是,我们想转义特殊字符
手动替换它们会有点脏,但如果没有解决方案......
The problem is, we'd like to escape LIKE special characters
Manually replacing them would be a bit dirty, but if there's no solution...
更简单的是:
It is more simple:
您可以只使用 zf 在字符串上使用的函数 addcslashes($value, "\000\n\r\'\"\032"); ,这将以 zf 使用的相同方式替换字符串,或者您可以(在mysql的情况下)使用mysql_real_escape_string,
无论哪种方式,你都不会使用数据库引用函数之一,
我确实想知道数据库类中是否有一种方法可以做到这一点,但我不知道应该有一个方法。
you could just use the function that zf uses on the string which is addcslashes($value, "\000\n\r\'\"\032"); that would replace the string in the same way that zf uses or you could (in the case of mysql) use mysql_real_escape_string.
either way you wouldn't use one of the db quote functions
i do wonder if there's a method in the db class to do this but i don't know of one there should be though.