简体   繁体   English

如何处理禁用准备好的语句

[英]How to handle disabling of prepared statements

I have a database class which user can instantiate. 我有一个用户可以实例化的数据库类。

class Foo extends PDO
{
    public function __construct($dsn, $username, $password)
    {
        parent::__construct($dsn, $username, $password);

        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
}

$dsn = 'mysql:host=127.0.0.1;dbname=dbdatabase;charset=utf8';
$dbConnection = new Foo($dsn, 'root', 'password');

However I need to disable the emulation of prepared statements in the constructor when the driver used is mysql : 但是,当使用的驱动程序是mysql时,我需要在构造函数中禁用对准备好的语句的仿真:

$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

For other drivers (that I know of) the emulation of prepared statements is disabled by default (like it should be). 对于其他驱动程序(据我所知),默认情况下会禁用预准备语句的仿真(应该如此)。 What would be the correct way to disable emulated prepared statements in my class. 在我的类中禁用模拟的预备语句的正确方法是什么?

  • Always add the line to disable emulated prepared statements? 总是添加该行以禁用模拟的准备好的语句吗? Will this have any side effects? 这会有副作用吗?
  • Do a stripos($dsn, 'mysql:') to find mysql in the dsn? 做一个stripos($dsn, 'mysql:')在dsn中找到mysql吗?
  • By using PDO::getAttribute('PDO::ATTR_DRIVER_NAME') ? 通过使用PDO::getAttribute('PDO::ATTR_DRIVER_NAME')吗?

I would avoid to do it globally, as you are not be able to determine the side effects of such parameters, you shouldn't do it. 我将避免在全局范围内这样做,因为您无法确定此类参数的副作用,因此不应该这样做。

By the way, you're trying to disable them only for mysql, so do it only for mysql. 顺便说一句,您正在尝试仅对mysql禁用它们,因此仅对mysql禁用它们。

I do think that using the PDO::ATTR_DRIVER_NAME is a fine option. 我确实认为使用PDO::ATTR_DRIVER_NAME是一个不错的选择。

I remember Doctrine\\DBAL using such things coupled with a driverMap to determine such things (need source for that) 我记得Doctrine\\DBAL使用这样的东西和driverMap来确定这样的东西(需要该信息的来源)

It also looks better than working with the DSN because, you may use a DSN alias with PDO , therefore mysql part may not be present. 它看起来也比使用DSN更好,因为,您可以将DSN别名与PDO ,因此mysql部分可能不存在。

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

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