What are the potential reasons for PHP scripts not being able to connect to a MySQL database, even though access is possible through phpMyAdmin?

The potential reasons for PHP scripts not being able to connect to a MySQL database, even though access is possible through phpMyAdmin, could be due to incorrect database credentials, server configuration issues, or firewall restrictions. To solve this issue, double-check the database connection details in your PHP script, ensure that the MySQL server is running and accessible, and verify that there are no firewall rules blocking the connection.

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