When developing PHP applications, database errors are one of the common exceptions, especially when using mysqli extension for database operations, how to effectively catch these errors and report them to the monitoring platform in time can help developers discover and solve problems faster. This article will introduce how to use mysqli::$errno to capture database errors and report error messages to Sentry or other monitoring platforms to ensure that your system remains stable and efficient.
mysqli::$errno is a very important property in the mysqli class. It is used to get the error code of the last database operation. If the operation is successful, errno will return 0 , and if an error occurs, the corresponding error code will be returned. Through this property, you can easily determine whether the database operation is successful.
When performing database operations, we usually perform error detection to promptly detect and handle exceptions. A simple example is as follows:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM nonexistent_table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
echo "SQL Error code: " . $mysqli->errno . " error message: " . $mysqli->error;
}
?>
The above code example shows how to use mysqli::$errno to catch errors and output corresponding error messages when making a query.
Sentry is a popular error tracking and monitoring platform that helps developers catch and handle errors in applications in real time. In order to send error messages to Sentry, the Sentry SDK is first required.
Use Composer to install the Sentry PHP SDK:
composer require sentry/sdk
Then, integrate Sentry into your code to report the error, the code is as follows:
<?php
require 'vendor/autoload.php';
// initialization Sentry
Sentry\init(['dsn' => 'https://your_sentry_dsn_url@m66.net']);
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM nonexistent_table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
// Catch database errors,And report it to Sentry
Sentry\captureException(new Exception("SQL Error code: " . $mysqli->errno . " error message: " . $mysqli->error));
echo "An error occurred,Reported to the monitoring platform。";
}
?>
In the above code, when mysqli executes a query, if an error occurs, the error message will be reported to the Sentry platform for monitoring through the Sentry\captureException() method.
In addition to Sentry, you can also send error messages to other monitoring platforms. Here are the basic integration methods using different platforms:
Bugsnag is another popular error monitoring tool. You can send errors to Bugsnag in the following ways:
require 'vendor/autoload.php';
// initialization Bugsnag
Bugsnag::start('your_bugsnag_api_key');
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM nonexistent_table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
// Catch database errors,And report it to Bugsnag
Bugsnag::notifyException(new Exception("SQL Error code: " . $mysqli->errno . " error message: " . $mysqli->error));
echo "An error occurred,Reported Bugsnag。";
}
LogRocket is another tool that supports front-end and back-end error tracking. If your PHP backend needs to integrate LogRocket, you can report error messages through the API:
require 'vendor/autoload.php';
// LogRocket integrated (Assumptions LogRocket PHP SDK Already exists)
LogRocket::init('your_logrocket_project_id');
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM nonexistent_table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
// Catch database errors,And report it to LogRocket
LogRocket::captureError("SQL Error code: " . $mysqli->errno . " error message: " . $mysqli->error);
echo "An error occurred,Reported LogRocket。";
}
Based on the above content, you can refer to the following complete example, use mysqli::$errno to catch database errors and report to Sentry:
<?php
require 'vendor/autoload.php';
// initialization Sentry
Sentry\init(['dsn' => 'https://your_sentry_dsn_url@m66.net']);
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM nonexistent_table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
// Catch database errors,And report it to Sentry
Sentry\captureException(new Exception("SQL Error code: " . $mysqli->errno . " error message: " . $mysqli->error));
echo "An error occurred,Reported Sentry。";
}
?>
By using the mysqli::$errno property, you can easily capture errors in database operations and report them to monitoring platforms such as Sentry, Bugsnag, or LogRocket. This way, you can promptly identify potential problems and enhance the stability and maintainability of your system.
When integrating these monitoring tools, remember to select the SDK that is suitable for your project and configure it accordingly accordingly according to the platform documentation.