MySQL Functions (PDO_MYSQL) MySQL (PDO)
&reftitle.intro; PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL databases. As of PHP 5.2.1, PDO_MYSQL uses emulated prepares by default. Formerly, PDO_MYSQL defaulted to native prepared statement support present in MySQL 4.1 and higher, and emulated them for older versions of the mysql client libraries. MySQL 8 When running a PHP version before 7.1.16, or PHP 7.2 before 7.2.4, set MySQL 8 Server's default password plugin to mysql_native_password or else you will see errors similar to The server requested authentication method unknown to the client [caching_sha2_password] even when caching_sha2_password is not used. This is because MySQL 8 defaults to caching_sha2_password, a plugin that is not recognized by the older PHP (mysqlnd) releases. Instead, change it by setting default_authentication_plugin=mysql_native_password in my.cnf. The caching_sha2_password plugin will be supported in a future PHP release. In the meantime, the mysql_xdevapi extension does support it. Beware: Some MySQL table types (storage engines) do not support transactions. When writing transactional database code using a table type that does not support transactions, MySQL will pretend that a transaction was initiated successfully. In addition, any DDL queries issued will implicitly commit any pending transactions.
&reference.pdo-mysql.configure; &reference.pdo-mysql.constants; &reference.pdo-mysql.ini;
PDO_MYSQL DSN Connecting to MySQL databases &reftitle.description; The PDO_MYSQL Data Source Name (DSN) is composed of the following elements: DSN prefix The DSN prefix is mysql:. host The hostname on which the database server resides. port The port number where the database server is listening. dbname The name of the database. unix_socket The MySQL Unix socket (shouldn't be used with host or port). charset The character set. See the character set concepts documentation for more information. Prior to PHP 5.3.6, this element was silently ignored. The same behaviour can be partly replicated with the PDO::MYSQL_ATTR_INIT_COMMAND driver option, as the following example shows. The method in the below example can only be used with character sets that share the same lower 7 bit representation as ASCII, such as ISO-8859-1 and UTF-8. Users using character sets that have different representations (such as UTF-16 or Big5) must use the charset option provided in PHP 5.3.6 and later versions. Setting the connection character set to UTF-8 prior to PHP 5.3.6 'SET NAMES utf8', ); $dbh = new PDO($dsn, $username, $password, $options); ?> ]]> &reftitle.examples; PDO_MYSQL DSN examples The following example shows a PDO_MYSQL DSN for connecting to MySQL databases: More complete examples: &reftitle.notes; Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.