我有一个小函数来计算表中的行,但是当我运行该函数时,我得到"致命错误:在P中的非对象上调用成员函数fetchColumn() :\ wamp \ www \ test \ class \ db.class.php在第34行" 。以下是我的代码:
<?php
class db {
public $pdo;
public function __construct() {
$this->pdo = new PDO("mysql:dbname=" . database_name . ";host=" . database_host . "", database_user, database_pass, array(
PDO::ATTR_PERSISTENT => true
));
}
public function countdata($field, $table, $where = "1") {
$ar = $this->pdo->query("SELECT COUNT(`".$field."`) FROM `" . $table . "` WHERE " . $where . "");
$cnt=$ar->fetchColumn(0);
return $cnt;
}
?>
答案 0 :(得分:0)
试试这个
$cnt=$ar->fetch();
或者通过提供别名
$ar = $this->pdo->query("SELECT COUNT(`".$field."`) as counts FROM `" . $table . "` WHERE " . $where . "");
$cnt=$ar->fetchColumn(0);
答案 1 :(得分:0)
你可能想确保$db-query()
您应该检查您对$db->query
的调用是否返回了有效的PDO对象