The PDO (PHP Data Objects) library uses an object-oriented approach to connect to databases. The main advantage of PDO is that it supports various types of databases (not just MySQL) and uses a consistent coding style for all of them.
To establish a connection, we need three key elements: a DSN (Data Source Name), a username,
and a password. With PDO, it is a best practice to wrap the connection in a try...catch block,
which captures potential errors (exceptions) if the connection fails.
try block contains the code we want to execute.catch block catches the error (PDOException) without revealing sensitive information to the user.