What are common errors encountered when trying to connect to MySQL in PHP scripts?

Common errors encountered when trying to connect to MySQL in PHP scripts include using incorrect credentials (such as the wrong username or password), incorrect hostname or port, or not having the necessary MySQL extension enabled in PHP. To solve this issue, double-check the credentials, hostname, and port, and ensure the MySQL extension is enabled in your PHP configuration.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_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";
?>