What are some common errors and solutions when connecting to a MySQL database using PHP?

Issue: One common error when connecting to a MySQL database using PHP is using incorrect credentials such as the wrong username, password, or database name. To solve this issue, make sure to double-check and verify that the credentials being used in the connection are correct.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>