How do you connect to a MySQL database using PHP

Connecting to a MySQL database using PHP is a fundamental skill for web developers. Below, you will find a simple example that illustrates how to make a connection to a MySQL database using the MySQLi extension in PHP.

<?php // Database configuration $host = 'localhost'; $user = 'username'; $password = 'password'; $database = 'database_name'; // Creating connection $connection = new mysqli($host, $user, $password, $database); // Check connection if ($connection->connect_error) { die("Connection failed: " . $connection->connect_error); } echo "Connected successfully"; ?>

MySQL PHP database connection MySQLi web development