How can a PHP developer effectively troubleshoot issues related to reading and updating data in a MySQL database using PHP scripts?

Issue: When reading or updating data in a MySQL database using PHP scripts, a common issue that may arise is incorrect SQL queries or connection errors. To effectively troubleshoot these issues, PHP developers can use error handling techniques, check for proper database connection, and validate SQL queries for any syntax errors.

// Example code snippet for troubleshooting MySQL database connection and query issues

// Establishing a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check database connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Example SQL query to read data from a table
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Check if query was successful
if ($result === false) {
    die("Error executing query: " . $conn->error);
}

// Example SQL query to update data in a table
$sql = "UPDATE table_name SET column_name = 'new_value' WHERE condition";
if ($conn->query($sql) === false) {
    die("Error updating record: " . $conn->error);
}

// Close database connection
$conn->close();