What steps should be taken to troubleshoot an "Access denied" error when connecting to a SQL server in PHP?

To troubleshoot an "Access denied" error when connecting to a SQL server in PHP, you should first ensure that the correct username and password are being used to connect to the database. Additionally, check that the user has the necessary permissions to access the database. Finally, verify that the host and port settings in the connection string are correct.

<?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";
?>