如何在 Zend Framework 中转义复杂的 sql?

发布于 2024-07-16 18:13:37 字数 645 浏览 13 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(7

幸福还没到 2024-07-23 18:13:37

最后一个选项对我来说效果很好,我还没有经历过它转义“%”。 因此 $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%

感性 2024-07-23 18:13:37

解决方案非常简单。 Zend_Db 有一个表达式类可以帮助您解决它。

$select = $this->select()
->where('value LIKE("?")', new Zend_Db_Expr('%' . $value . '%'))

$this->fetchAll( $select );

The solution is really simple. Zend_Db has een Expression class that helps you work arround it.

$select = $this->select()
->where('value LIKE("?")', new Zend_Db_Expr('%' . $value . '%'))

$this->fetchAll( $select );
心的憧憬 2024-07-23 18:13:37

您可以在 SQL 级别进行 $input 的串联:

$sql=$DB->quoteInto("SELECT * FROM t WHERE myname LIKE '%'|| ? ||'%'",$input);

不幸的是,当您希望 $input 能够包含文字“%”或“_”字符时,这不可用。 要解决这个问题,请指定一个显式的 LIKE-ESCAPE 字符并自行转义它们:(

$inputlike= '%'.preg_replace('[%_=]', '=$0', $input).'%';
$sql=$DB->quoteInto("SELECT * FROM t WHERE myname LIKE ? ESCAPE '='", $inputlike);

它可以是任何字符,不一定是“=”。这也解决了 MySQL 中未指定时 ESCAPE 默认为“\”的错误。)

不幸的是,SQL Server 也将“[”字符视为特殊字符,以执行类似正则表达式的字符组。 因此,如果您的数据库是 SQL Server,则必须在 preg_replace 的组中包含“[”。 不幸的是,在其他不需要转义的 DBMS 上,转义 '[' 并不是有效的 ANSL SQL。

You can do the concatenation of $input at the SQL level:

$sql=$DB->quoteInto("SELECT * FROM t WHERE myname LIKE '%'|| ? ||'%'",$input);

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:

$inputlike= '%'.preg_replace('[%_=]', '=$0', $input).'%';
$sql=$DB->quoteInto("SELECT * FROM t WHERE myname LIKE ? ESCAPE '='", $inputlike);

(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.

半岛未凉 2024-07-23 18:13:37

很简单:

$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?",'%' . $input . '%');

//Will output: SELECT FROM t WHERE myname LIKE '%inputtedvalue%'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?",'%' . $input);

//Will output: SELECT FROM t WHERE myname LIKE '%inputtedvalue'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?", $input . '%');

//Will output: SELECT FROM t WHERE myname LIKE 'inputtedvalue%'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?", $input);

//Will output: SELECT FROM t WHERE myname LIKE 'inputtedvalue'

问题是什么?

:)

It is very simple:

$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?",'%' . $input . '%');

//Will output: SELECT FROM t WHERE myname LIKE '%inputtedvalue%'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?",'%' . $input);

//Will output: SELECT FROM t WHERE myname LIKE '%inputtedvalue'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?", $input . '%');

//Will output: SELECT FROM t WHERE myname LIKE 'inputtedvalue%'


$sql=$DB->quoteInto("SELECT *
                     FROM t
                     WHERE myname LIKE ?", $input);

//Will output: SELECT FROM t WHERE myname LIKE 'inputtedvalue'

What is the prolem?

:)

¢蛋碎的人ぎ生 2024-07-23 18:13:37

问题是,我们想转义特殊字符
手动替换它们会有点脏,但如果没有解决方案......

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...

云柯 2024-07-23 18:13:37

更简单的是:

$table->select()->where("myname LIKE ?", '%'.$input.'%');

It is more simple:

$table->select()->where("myname LIKE ?", '%'.$input.'%');
倦话 2024-07-23 18:13:37

您可以只使用 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.

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