PDO致命错误:在非对象上调用成员函数fetchColumn()

时间:2012-01-20 21:35:14

标签: php pdo

尝试迁移到PDO无法弄清楚这个

有什么问题
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
$sql = "SELECT COUNT(*) FROM users WHERE repo=? AND list=? AND email=?";
$q=$dbh->prepare($sql);
$res = $q->execute(array($repo, $list, $email));
$v = $res->fetchColumn();

2 个答案:

答案 0 :(得分:1)

PDOStatement::execute()返回TRUEFALSE(请参阅here),因此$res被设置为布尔值,然后您尝试调用一个方法就可以了。试试这个:

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
$sql = "SELECT COUNT(*) FROM users WHERE repo=? AND list=? AND email=?";
$q=$dbh->prepare($sql);
if (($res = $q->execute(array($repo, $list, $email))) === FALSE) {
    echo 'Query failed!';
    exit;
}
$v = $q->fetchColumn();

答案 1 :(得分:0)

$q->execute(...)

仅返回布尔值。 正确的方法:

$q = $dbh->prepare($sql);

if ($q->execute($repo, $list, $email) == TRUE) {
 $v = $q->fetchColumn();
} else {
 //error
}