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:
$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? - By using
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.
I do think that using the
PDO::ATTR_DRIVER_NAMEis a fine option.I remember
Doctrine\DBALusing such things coupled with a driverMap to determine such things (need source for that)It also looks better than working with the DSN because, you may use a DSN alias with
PDO, thereforemysqlpart may not be present.