I'm updating some legacy PHP code and trying to get some decent error logging. All calls to the DB now go through this function:
private function dbQuery($sql) {
if (DEBUG) print("\n" . $sql . "\n");
$result = pg_query($this->dbh, $sql);
if ($result == FALSE) print("DB Error: " . pg_last_error($this->dbh) . "\n");
return $result;
}
But, in at least one case, the error is appearing in the console all by itself (as a PHP Warning), while pg_last_error returns nothing (even though the result of the pg_query call is FALSE). Actual output:
insert into pull_count (show_pull_item_id, count_pulled, created, modified) values (1076028, 1, NOW(), NOW())
PHP Warning: pg_query(): Query failed: ERROR: permission denied for relation pull_count in /var/www/html/src/backend/engine.php on line 1740
DB Error:
And in the calling function, where we again call pg_last_error(), we still get nothing.
So... what's the deal? Why is the error going to the console, and not to pg_last_error -- and how can I fix it?
OK, it turns out I'm an idiot... the code wasn't getting pushed to the server properly, so I was testing older code that looked like this:
Note the failure to include the DB connection handle in the pg_last_error call. This is why it failed to return any result.
With that corrected, I still get a PHP Warning in the console, but I also get a proper pg_last_error, so all is right with the world.