I’m trying to get a project set up and working but I have some issues with my PDO connection. No big deal, however, it doesn’t matter what logs I’m trying to find – they are all empty.
within the php file itself, I have
ini_set('error_log', '/tmp/php_error.php');
.within my
sites-enabled
config I have the error log defined under${APACHE_LOG_DIR}/error-domain.log
both files are empty and don’t track any errors.
I’m not a debian specialist by any means, what am I missing here? are there any other log directories to check? what could cause the system to not write any error messages.
Advertisement
Answer
Make sure PDO is configured to throw errors, otherwise SQL queries may fail silently and you’ll see nothing in your logs.
Add this to your code just after you create the connection (in this example $conn
is a variable created from the PDO constructor (e.g. $conn = new PDO(...
etc.).
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Once this is configured, SQL errors resulting from a PDO command will be thrown as PHP exceptions. If PHP’s normal error logging is configured correctly, these errors will then appear in your PHP error_log file.
Documentation: https://www.php.net/manual/en/pdo.error-handling.php