douxiduan8344 2017-02-08 16:38
浏览 241

pg_query错误直接进入控制台,而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("
" . $sql . "
");
        $result = pg_query($this->dbh, $sql);
        if ($result == FALSE) print("DB Error: " . pg_last_error($this->dbh) . "
");
        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条回答 默认 最新

  • duancheng6500 2017-02-09 17:20
    关注

    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("
    " . $sql . "
    ");
        $result = pg_query($this->dbh, $sql);
        if ($result == FALSE) print("DB Error: " . pg_last_error() . "
    ");
        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.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值