gpt4 book ai didi

php - 为什么 PDO 不允许同名的多个占位符?

转载 作者:可可西里 更新时间:2023-11-01 07:01:15 24 4
gpt4 key购买 nike

我正在使用 PHP 和带有 PDO 的 MySQL。有时我需要准备一个语句,其中一个变量(占位符)在此查询中使用了不止一次。

例子:

SELECT * FROM messages WHERE from_id = :user OR to_id = :user

但是,如果我尝试准备此声明,我将遇到错误,因此我需要以如下方式执行此操作:

SELECT * FROM messages WHERE from_id = :user1 OR to_id = :user2

要调用这个语句,我需要有一个这样的数组:

array('user1'=>$user_id, 'user2'=>$user_id);

在我看来这太愚蠢了!为什么 MySQL(PDO?)不允许我多次使用一个占位符并强制我使用需要更多控制的额外变量?!

如果查询相对简单(就像我在上面发布的那样),这可以很容易地处理,但现在我构建了一个查询,其中使用了 5 (!!!) 次单个变量。每次我添加占位符时,我都需要在很多地方检查代码以使其正常。

是否有任何设置或调整可以绕过此问题?

最佳答案

Is there any setting or a tweak to bypass this?

是的,有。 You can turn emulation mode ON并能够多次使用相同的占位符。

因此,仅当仿真关闭时才会观察到所描述的行为。我真的不明白为什么会这样,但这是 Wez Furlong(PDO 作者)的解释:

The change was made for two reasons; first and foremost, if you re-use the same variable in a bind, it is possible to induce a crash when using some drivers. It’s not possible to guarantee to do the right thing, and having a way to trigger a crash can sometimes be used as an attack vector for a security exploit.

The second reason is that of portability. Some drivers would internally perform this check and error out. If you code against the drivers that don’t enforce this, then your code won’t work on those that don’t.

http://paul-m-jones.com/archives/243#comment-740

关于php - 为什么 PDO 不允许同名的多个占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42244086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com