In general, when defining a PDO class, we specify the connection events with a DSN, or “Data Source Name”. We express which database driver we will connect to and our information with DSN. In the other two parameters, we enter our database username and password.

$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

For DSN statements of other drivers, see here.

$dsn = 'mysql:host=localhost;dbname=test';
$user = 'dbuser';
$password = 'mypassword';
try {
    $db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();

In this way, we catch connection errors.