How can proper error handling techniques, such as displaying MySQL error messages, enhance the debugging process in PHP scripts?
Proper error handling techniques, such as displaying MySQL error messages, can enhance the debugging process in PHP scripts by providing more detailed information about what went wrong in case of errors. This can help developers quickly identify and fix issues in their code.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Perform MySQL query
$result = $mysqli->query("SELECT * FROM table");
// Check for query errors
if (!$result) {
die("Error: " . $mysqli->error);
}
// Fetch and display results
while ($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close connection
$mysqli->close();