DSN Options
The Data Source Name (DSN) string encodes connection details. Additional options control persistent connections, timeouts, and character sets.
The Data Source Name (DSN) string encodes connection details. Additional options control persistent connections, timeouts, and character sets.
<?php
// Full MySQL DSN with all options
$dsn = "mysql:host=db.example.com;port=3306;dbname=myapp;charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => false, // Avoid persistent connections in web
PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '+00:00'",
PDO::ATTR_TIMEOUT => 5, // Connect timeout seconds
];
$pdo = new PDO($dsn, "username", "password", $options);
Set the connection timezone to UTC via MYSQL_ATTR_INIT_COMMAND to avoid timezone-related date bugs.