How can debugging techniques such as error_reporting and mysql_error() be utilized to troubleshoot PHP scripts that interact with a MySQL database?

When troubleshooting PHP scripts that interact with a MySQL database, error_reporting can be set to display all errors and warnings, providing valuable information about any issues. Additionally, using mysql_error() can help identify specific errors related to database queries, allowing for targeted troubleshooting and debugging.

// Set error reporting to display all errors and warnings
error_reporting(E_ALL);

// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = mysqli_connect($servername, $username, $password, $dbname);

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

// Perform database query
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);

// Check for errors in query execution
if (!$result) {
    echo "Error: " . mysqli_error($conn);
}

// Close database connection
mysqli_close($conn);