pg_query error going directly to console, while pg_last_error returns nothing

…衆ロ難τιáo~ 提交于 2019-12-12 03:56:04

问题


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?


回答1:


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:

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() . "\n");
    return $result;
}

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.



来源:https://stackoverflow.com/questions/42118755/pg-query-error-going-directly-to-console-while-pg-last-error-returns-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!