This tutorial aims at teaching you how to effectively handle database errors in PHP. By learning how to handle these errors, you can improve your application's reliability and provide a better user experience.
By the end of this tutorial, you'll be able to:
- Understand different types of database errors
- Implement error handling in PHP
- Use exceptions to handle database errors
Database errors can occur due to several reasons, such as incorrect SQL syntax, wrong data input, server failure, etc. Handling these errors is vital to prevent your application from crashing and to maintain data integrity.
PHP provides several ways to handle errors that include error reporting, using die()
function or custom error handling functions. However, when dealing with databases, the most recommended way is to use Exceptions.
An exception is a special kind of object that PHP will "throw" when an error occurs. The exception can then be "caught" and handled in a way that allows your application to continue running, or at least fail gracefully.
Let's see how to use exceptions to handle errors when interacting with a MySQL database.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
try {
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
$conn->query($sql);
echo "Record deleted successfully";
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
$conn->close();
?>
In this example, if an error occurs when executing the SQL query, an exception will be thrown. This exception is then caught and handled by echoing the error message.
In this tutorial, we've learned about database errors and how to handle them in PHP using exceptions. You should now be able to improve your application's reliability and prevent unwanted crashes.
Solutions
1.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new mysqli($servername, $username, $password);
echo "Connected successfully";
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
try {
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
$conn->query($sql);
echo "New record created successfully";
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
$conn->close();
?>
In the first script, we are trying to establish a connection with the database and if any error occurs, we catch it and display the error message. In the second script, we are inserting data into a table. If there's any error during the process, we catch it and display the error message.