In this tutorial we will understand how to connect database in PHP so here I am making the object MySQLi and passing host name, username, password and DB name so this will help you to connect to database.


db.php

<?php
$conn = new mysqli("localhost", "root", "", "db_name");
// Check connection
if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: " . $conn->connect_error;
    exit();
}
?>

$conn->connect_error if there is problem to connect to db it will show the error message