pg_query错误直接转到控制台,而pg_last_error不返回任何内容 [英] pg_query error going directly to console, while pg_last_error returns nothing
问题描述
我正在更新一些传统的PHP代码,并尝试获得一些体面的错误记录。所有对数据库的调用现在都通过这个功能来实现:
private function dbQuery($ sql){
if(DEBUG )print(\\\
。$ sql。\\\
);
$ result = pg_query($ this-> dbh,$ sql);
if($ result == FALSE)print(DB Error:。pg_last_error($ this-> dbh))\\\
);
return $ result;
}
但是,至少有一种情况下,控制台中出现错误本身(作为PHP警告),而pg_last_error不返回任何东西(即使pg_query调用的结果为FALSE)。实际输出:
insert into pull_count(show_pull_item_id,count_pulled,created,modified)values(1076028,1,NOW(),NOW ())
PHP警告:pg_query():查询失败:错误:对/ lib / b $ b
在调用函数中,我们再次调用pg_last_error(),我们仍然没有收到。 >
那么什么交易?为什么错误进入控制台,而不是pg_last_error - 而且如何修复?
原来我是一个笨蛋...代码没有被正确地推送到服务器,所以我正在测试这样的旧代码:
私有函数dbQuery($ sql){
if(DEBUG)print(\\\
。$ sql。\\\
);
$ result = pg_query($ this-> dbh,$ sql);
if($ result == FALSE)print(DB Error:。pg_last_error()。\\\
);
return $ result;
}
请注意在pg_last_error调用中没有包含DB连接句柄。这就是为什么它没有返回任何结果。
正确的是,我仍然在控制台中收到一个PHP警告,但是我也一个适当的pg_last_error,所以都是正确的世界。
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:
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.
这篇关于pg_query错误直接转到控制台,而pg_last_error不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!