PDO MySQL: Enable or Disable PDO::ATTR_EMULATE_PREPARES
Introduction
When working with PDO MySQL, a critical decision is whether to enable or disable PDO::ATTR_EMULATE_PREPARES. This article explores the trade-offs and recommendations based on the specific concerns of performance and security.
Performance Considerations
With Emulation Enabled (EMULATE_PREPARES = true)
With Emulation Disabled (EMULATE_PREPARES = false)
Security Considerations
Error Reporting
Additional Considerations
Recommendation
Based on the considerations above, the following recommendations are made:
Example Connection Function
To implement these recommendations, consider using the following connection function:
<?php function connect_PDO($settings) { $dbh = new PDO( 'mysql:' . implode(';', $settings), $settings['user'], $settings['pass'], [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => (version_compare($dbh->getAttribute(PDO::ATTR_SERVER_VERSION), '5.1.17', '<')) ] ); return $dbh; }
By modifying the PDO::ATTR_EMULATE_PREPARES setting based on your MySQL version, you can achieve optimal balance between performance and security.
The above is the detailed content of PDO MySQL's `PDO::ATTR_EMULATE_PREPARES`: To Enable or Disable?. For more information, please follow other related articles on the PHP Chinese website!