PDO Connection String Builder

Build a PHP PDO connection for MySQL, PostgreSQL, or SQLite.

Advertisement

What is a PDO DSN?

A PDO data source name, or DSN, tells PHP which database driver to use and where the database is located. It is passed as the first argument when creating a PDO connection.

Each database uses a slightly different DSN format. MySQL commonly includes a host, port, database name, and character set, while PostgreSQL uses host, port, and database name. SQLite connections point to a local database file.

Build connection code quickly

Select your database driver and enter the relevant settings. The generator updates the DSN and complete PHP connection snippet as you type, ready to copy into a project or test script.

For MySQL, using utf8mb4 is recommended because it supports the full range of UTF-8 characters. The generated code also sets exception mode so connection and query errors can be handled clearly.

Keep credentials out of source code

The values entered here stay in your browser. In a production application, store credentials in environment variables or a secret manager instead of committing them directly to a repository.

Advertisement