# Username and Password in PDO DSN

> Section in “New and Extended Functionality › I/O and Filesystem” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/io-and-filesystem/username-and-password-in-pdo-dsn/>

To connect to a database, you (hopefully) have to specify a username and password. Since PHP 7.4, when using `pdo` to connect to the database, you specify the username and password as part of the DSN:

``` php
new PDO('mysql:host=localhost;port=3306;dbname=test;user=thephpcc;password=*******');
```

We always use `*******` as password, by the way.

Before PHP 7.4, specifiying username and password only was possible when connecting to a PostgreSQL database. Note that specifying username and password as constructor parameters is still possible:

``` php
new PDO('mysql:host=localhost;port=3306;dbname=test', 'thephpcc', '*******');
```

Username and password specified in the constructor have precedence over the DSN.

---

Previous: [File Deletion on Windows](https://php7explained.com/new-and-extended-functionality/io-and-filesystem/file-deletion-on-windows/index.html.md) | Next: [New Function fpm_get_status() in PHP-FPM](https://php7explained.com/new-and-extended-functionality/io-and-filesystem/new-function-fpm_get_status-in-php-fpm/index.html.md)
