How can debugging techniques like echoing variables be used to troubleshoot issues related to MySQL connections in PHP scripts?

To troubleshoot MySQL connection issues in PHP scripts, you can use debugging techniques like echoing variables to check the connection parameters and error messages. By echoing out variables such as the host, username, password, and database name, you can verify that the correct values are being used. Additionally, you can echo out any error messages returned by the MySQL functions to pinpoint the root cause of the connection problem.

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

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

if (!$conn) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
    echo "Connected successfully!";
}
?>