: Report connection and query errors through the exception mechanism ( PDOException ). The code can be captured using try...catch , and the exception information contains detailed error descriptions.
mysqli : Use properties and methods such as $mysqli->connect_error and $mysqli->connect_errno to report connection errors. Errors will not automatically throw exceptions and need to be detected and processed actively.
In other words, PDO relies more on exception mechanisms, while mysqli relies more on explicit error checking.
try {
$pdo = new PDO('mysql:host=gitbox.net;dbname=testdb;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$mysqli = new mysqli('gitbox.net', 'username', 'password', 'testdb');
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connection successfully";
Note: mysqli gets the connection error message through $mysqli->connect_error , and will not automatically throw exceptions and must be checked manually.
PDO uses exceptions, and the code structure is simpler and safer.
mysqli needs to explicitly judge $mysqli->connect_error or $mysqli->connect_errno .
If you are used to using try...catch to catch exceptions, you need to adjust the error handling logic after switching to mysqli.
PDO directly specifies the character set in the DSN connection string, such as charset=utf8 , mysqli requires additional calls:
$mysqli->set_charset('utf8');
Otherwise, there will be problems with Chinese garbled code.
PDO supports many connection options, while mysqli supports some options. for example:
PDO can be enabled with PDO::ATTR_PERSISTENT .
mysqli requires additional processing or use different ways to enable persistent connections.
The PDO preprocessing statement uses $stmt = $pdo->prepare($sql); to support named parameter binding.
The mysqli preprocessing statement uses $stmt = $mysqli->prepare($sql); , and only question mark placeholders are supported.
If the original code uses a large number of named parameters, it needs to be rewritten as a question mark.
Mysqli does not throw exceptions by default. If you want to simulate exception capture of PDO, you can enable the mysqli exception throw function:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
After opening, mysqli will throw mysqli_sql_exception , which is easier to handle errors in a unified way.
project | PDO | mysqli |
---|---|---|
Error handling | Exception catch (try...catch) | Externally check $connect_error to enable exceptions |
Connection string format | DSN string (charset can be set) | Set charset separately, you need to call set_charset() |
Preprocessing parameter binding | Support naming parameters | Only question mark placeholders are supported |
Persistent connection support | Set by options | Need different methods to implement |
In short, switching from PDO to mysqli is not only a replacement connection method, but also comprehensively considering the differences in error handling, encoding settings, preprocessing statements, etc. to avoid implicit bugs caused by inadaptation.