$connection = new mysqli("localhost", "username", "password", "database");
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
echo "Connected successfully";
Output:
Connection failed: Access denied for user 'username'@'localhost'
In this example, $connection->connect_error is used to check if the connection has failed. If an error is found, the script will exit and display the error message.
$sql = "SELECT * FROM invalid_table"; // Example of an incorrect query
$result = $connection->query($sql);
if (!$result) {
echo "Query failed: " . $connection->error;
}
Output:
Query failed: Table 'database.invalid_table' doesn't exist
In this example, we intentionally run an invalid query to demonstrate how errors can be captured using $connection->error.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$connection = new mysqli("localhost", "username", "password", "database");
$result = $connection->query("SELECT * FROM invalid_table"); // Invalid query
} catch (mysqli_sql_exception $e) {
echo "Error: " . $e->getMessage();
}
Output:
Error: Table 'database.invalid_table' doesn't exist
In this example, when an error occurs, an exception is thrown and caught by the catch block. This method of error handling is more structured and fits well into object-oriented programming practices.
$stmt = $connection->prepare("INSERT INTO users (name, age) VALUES (?, ?)");
if (!$stmt) {
echo "Prepare failed: (" . $connection->errno . ") " . $connection->error;
} else {
$stmt->bind_param("si", $name, $age);
$stmt->execute();
}
Output:
Prepare failed: (1146) Table 'database.users' doesn't exist
Here, $connection->prepare() checks if the statement preparation was successful. If not, the error code and message are displayed.
$student_name = "John Doe";
$student_marks = 105; // Invalid marks greater than 100
try {
// Check if marks are valid
if ($student_marks > 100) {
throw new Exception("Marks entered ($student_marks) exceed 100.");
}
// Prepare the SQL query to insert the student's record
$stmt = $connection->prepare("INSERT INTO student (name, marks) VALUES (?, ?)");
if (!$stmt) {
die("Prepare failed: (" . $connection->errno . ") " . $connection->error);
}
// Bind parameters and execute the statement
$stmt->bind_param("si", $student_name, $student_marks);
$stmt->execute();
echo "Student record added successfully!";
} catch (Exception $e) {
// Handle the exception for marks exceeding 100
echo "Error: " . $e->getMessage();
}
Error: Marks entered (105) exceed 100.
error_log(message, type, destination, headers)
message: The error message to be logged.Type | Description |
---|---|
0 | Logs the error message to the PHP system log (usually the web server's error log or syslog). |
1 | Sends the error message via email to the address specified in the destination parameter. |
2 | Not used in PHP. Reserved for future use. |
3 | Appends the error message to a file specified in the destination parameter. |
try {
// Check for connection error
if ($connection->connect_error) {
throw new Exception("Connection failed: " . $connection->connect_error);
}
// Example SQL query
$query = "SELECT * FROM non_existent_table";
if (!$result = $connection->query($query)) {
throw new Exception("Query failed: " . $connection->error);
}
} catch (Exception $e) {
// Log error to PHP error log
error_log($e->getMessage(), 3, "error_log.txt");
// Display user-friendly message
echo "An error occurred. Please check the log file.";
}
$log_file = 'error_log.txt';
$error_message = date('Y-m-d H:i:s') . " - Error: " . $connection->error . "\n";
// Log error to file
file_put_contents($log_file, $error_message, FILE_APPEND);
echo "Error has been logged.";
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.