What are common database connection errors in PHP applications?

Common database connection errors in PHP applications include incorrect credentials, server unavailability, and misconfigured database settings. To solve these issues, ensure that the database credentials are correct, the database server is running and accessible, and the database settings in the PHP code match the actual database configuration.

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

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

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