What common mistakes can lead to MySQL connection errors in PHP scripts?

Common mistakes that can lead to MySQL connection errors in PHP scripts include using incorrect host, username, password, or database name in the connection settings, not enabling the MySQL extension in PHP configuration, and not handling connection errors gracefully. To solve this issue, ensure that the connection settings are correct, enable the MySQL extension in PHP configuration, and implement error handling to catch and display connection errors.

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

// Create connection
$conn = mysqli_connect($host, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";
?>