Skip to content
WORDPRESS, PHP & DATABASE

Database Connection String Generator – PHP

Generate PDO, MySQLi and WordPress connection templates.

  • Free
  • No signup
  • Runs in your browser

This tool runs in your browser. The text you enter is processed on your device and is not sent to NHP Learning.

Database Connection String Generator

About Database Connection String Generator

Connecting PHP to MySQL needs the same four facts in every framework: host, database name, user and password. The syntax is different for PDO, MySQLi and the WordPress wp-config.php file.

Enter the host, the database name and the user separated by commas. The tool writes a PDO connection with the utf8mb4 character set and exception mode, a MySQLi connection and the four DB_ constants for WordPress. The password is always a placeholder, so you never type a real password into a web form.

How to use Database Connection String Generator

  1. Type host, database and user separated by commas, for example localhost, my_database, my_user.
  2. Click Run Tool to generate the three variants.
  3. Copy the one you need and replace YOUR_PASSWORD in your own file.

Example

Input

localhost, nhp_learning, nhp_user

Output

PDO (MySQL):
$pdo = new PDO( 'mysql:host=localhost;dbname=nhp_learning;charset=utf8mb4', 'nhp_user', 'YOUR_PASSWORD', array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) );

MySQLi:
$mysqli = new mysqli( 'localhost', 'nhp_user', 'YOUR_PASSWORD', 'nhp_learning' );

WordPress wp-config.php:
define( 'DB_NAME', 'nhp_learning' );
define( 'DB_USER', 'nhp_user' );
define( 'DB_PASSWORD', 'YOUR_PASSWORD' );
define( 'DB_HOST', 'localhost' );

Common uses

  • Writing a first PDO connection for a PHP project on cPanel hosting.
  • Setting up wp-config.php after moving a WordPress site.
  • Comparing how PDO and MySQLi connect.

Privacy and safety

This tool runs in your browser. The text you enter is processed on your device and is not sent to NHP Learning.

Frequently asked questions

PDO or MySQLi?

MySQLi works only with MySQL and MariaDB. PDO supports many databases and named parameters. Both support prepared statements, so either is safe when used correctly.

What host should I use on shared hosting?

Most shared hosts use localhost. Some use a separate host name, which is shown in the hosting control panel next to the database details.

Why utf8mb4?

It stores the full Unicode range, including emoji and all Indian scripts. The older utf8 charset in MySQL only handles three bytes per character.

Learn to build this yourself

Free tutorials and courses on NHP Learning cover PHP, JavaScript, SQL and WordPress, from the basics to real projects.