How to connect to MySQL using PDO

  1. Example
  2. Credentials exlained
  3. Connection options explained
  4. Handling errors
  5. Creating the connection
  6. Don'ts
  7. Accessing the newly created connection
  8. Comments (13)

In this example we will learn how to properly connect to Mysql database using PDO. It is based on the information provided in the main article on PDO but with additional explanations.

Surprisingly, there is no single state-of-the-art connection example in the PHP manual. Instead, different connection options are discussed in different chapters, which makes it hard for the learner to get a single robust example that is ready to use. Below you will find such an example, as well as the explanation of all the options used.


$host '';
$db   'test';
$user 'root';
$pass '';
$port "3306";
$charset 'utf8mb4';

$options = [
$dsn "mysql:host=$host;dbname=$db;charset=$charset;port=$port";
try {
$pdo = new \PDO($dsn$user$pass$options);
} catch (\
PDOException $e) {
     throw new \
PDOException($e->getMessage(), (int)$e->getCode());

Credentials exlained

First of all we are defining variables that contain connection credentials. This set is familiar to anyone who were using the old mysql_connect() function, save for $charset may be, which was rarely used (although it should have been).

Note it's a good idea to store connection variables ($host, $db etc.) in a separate file. This way you'll be able to have two versions of your code, one for the local server and one for the remote.

Connection options explained

Next we are creating an array with PDO options that are either critically important or just make your experience with PDO much better.

Handling errors

An uncaught exception is converted to a PHP fatal error. It is not a problem by itself, errors are for the good, and we desperately need this one to get the idea what's wrong with our database. But such a converted error contains a stack trace added to the error message, which in case of PDO connection error would include the constructor parameters which happen to be the database credentials. Again, it shouldn't be a problem, as on a live site displaying errors should be always turned off anyway, but we are humans and we make mistakes. So, to avoid even a chance to reveal the credentials, we are catching the Exception and immediately re-throwing them. So, now the stack trace begins on the throw line and doesn't contain the database credentials.

Creating the connection

Having all the options and credentials set, we can finally proceed to creating a connection. To do so we need to create an instance of PDO class for which we need to supply 4 parameters of which the first one, called "DSN" being most important.

DSN is a semicolon-delimited string, consists of param=value pairs, that begins from the driver name and a colon:

^    ^ colon         ^param=value pair    ^semicolon  

Note that it's important to follow the proper format - no spaces or quotes or other decorations have to be used in DSN, but only parameters, values and delimiters, as shown in the manual.

Beside DSN, we are using $user, $pass and $options variables defined above.

We are wrapping the creation of the PDO instance into a try..catch statement in order to be aware of the possible error, but without the risk of revealing the database credentials.


Beside things that you should do, there are always things that you should do not. Some of them we will discuss below.

Accessing the newly created connection

There is one thing that makes PDO more complex than old mysql_connect related stuff. Although one was able to use mysql_query anywhere in the code, without taking care of the connection which was magically supplied by PHP, with PDO one should always make sure that once created PDO instance is available in each part of their script.

There are many different strategies to have this done, but mostly used are:

Related articles: