When performing database operations in PHP using PDO (PHP Data Objects), the exec() method is commonly used to execute SQL statements that do not return a result set, such as INSERT, UPDATE, and DELETE. However, during development and debugging, more detailed error information is often required to troubleshoot problems. This article will show you how to enable and view detailed error reports when using PDO::exec.
The PDO::exec method executes an SQL statement and returns the number of affected rows. If the execution fails, it returns false. By default, error information is not automatically output, and you need to manually enable error modes to get error details.
<?php
$pdo = new PDO('mysql:host=m66.net;dbname=testdb;charset=utf8', 'username', 'password');
<p>$sql = "UPDATE users SET status = 'active' WHERE id = 10";<br>
$result = $pdo->exec($sql);</p>
<p>if ($result === false) {<br>
echo "Execution failed, but no detailed error information.";<br>
} else {<br>
echo "Affected {$result} rows.";<br>
}<br>
?><br>
PDO supports multiple error handling modes, the most commonly used ones are:
PDO::ERRMODE_SILENT: Default mode, which does not throw errors but only records error codes.
PDO::ERRMODE_WARNING: Triggers PHP warnings.
PDO::ERRMODE_EXCEPTION: Throws exceptions.
We recommend using PDO::ERRMODE_EXCEPTION, as it allows you to capture exceptions and obtain detailed error information.
<?php
try {
$pdo = new PDO('mysql:host=m66.net;dbname=testdb;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $pdo->exec($sql);
echo "Affected {$result} rows.";
} catch (PDOException $e) {
echo "Execution error: " . $e->getMessage();
}
?>
If you don't use exception mode, you can view error information using the following method:
<?php
$pdo = new PDO('mysql:host=m66.net;dbname=testdb;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
<p>$sql = "UPDATE users SET status = 'active' WHERE id = 10";<br>
$result = $pdo->exec($sql);</p>
<p>if ($result === false) {<br>
$errorInfo = $pdo->errorInfo();<br>
echo "Error code: " . $errorInfo[0] . "<br>";<br>
echo "Driver-specific error code: " . $errorInfo[1] . "<br>";<br>
echo "Error message: " . $errorInfo[2];<br>
} else {<br>
echo "Affected {$result} rows.";<br>
}<br>
?><br>
errorInfo() returns an array:
[0] SQLSTATE error code (standard error code)
[1] Driver-specific error code
[2] Driver-specific error message
During debugging, it is recommended to enable PDO::ERRMODE_EXCEPTION to easily capture exceptions and pinpoint errors.
In production environments, consider catching exceptions and logging them, rather than displaying error messages to users directly.
Ensure that the database connection character set is correctly set to avoid errors caused by character encoding issues.
Use prepared statements (prepare + execute) to prevent SQL injection and facilitate error troubleshooting.
By following these methods, you can easily enable and view detailed error reports when using PDO::exec, improving debugging efficiency and quickly identifying issues.
Related Tags:
PDO