douzi6992 2012-04-27 16:35
浏览 148
已采纳

PDOStatement fetchColumn()的PHP致命错误

I can't understand this error, could someone please explain it to me?

PHP Fatal error: Call to a member function fetchColumn() on a non-object in /user/dal.php on line 27

I am trying to get the count of the last query, by using regex to perform a new SELECT COUNT(*) query on the last query. The problem is $stmt->fetchColumn() causes a fatal error and then I get Error 500.

function dbRowsCount($sql) {
    global $db;
    $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
    if (preg_match($regex, $sql, $output) > 0) {
        $stmt = $db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
        return $stmt->fetchColumn();
    }

    return false;
}

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doulu1325 2012-04-27 16:37
    关注

    You should check that your call to $db->query returns a valid PDO object. Something like this:

    $stmt = $db->query(...);
    if (! $stmt) {
        print_r($db->errorInfo());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题