我看了,发现我在这里做错了什么。我有一个使用以下方法创建的数据库类:
public function db_fetch_dataset($sql_qry)
{
$log= new log_class;
$db_conn = self::_connect();
try{
$results = $db_conn->query($sql_qry);
return $results;
}catch(PDOException $e){
log_class::save_to_log($e,__LINE__,__FILE__);
}
$db_conn = null;
}
我在这里使用它:
$db= new database_class;
$sql_parties ="SELECT (SELECT political_party
FROM political_party_tbl
WHERE political_party_abbr = PARTY) as PARTY
,count_of_voters
FROM vrm_count_of_registered_voters_by_party_vw
ORDER BY PARTY";
$results = $db->db_fetch_dataset($sql_parties);
while($row= $results ->fetch(PDO::FETCH_ASSOC))
{
echo "<h4>".$row['PARTY'].": ".number_format($row['count_of_voters'])."</h4>\n";
}
我收到以下错误: “致命错误:在非对象上调用成员函数fetch()”
有人会善意地告诉我我做错了什么或指导我找到答案吗? 谢谢!
添加 - fyi:查询本身不会导致错误。它在SQL Server Manager中运行良好。
答案 0 :(得分:1)
请更正声明:
WHERE political_party_abbr] = PARTY) as PARTY
要:
WHERE political_party_abbr = PARTY) as PARTY
答案 1 :(得分:0)
好的,让它运转起来。这是有用的:
public function db_fetch_dataset($sql_qry)
{
$log= new log_class;
$db_conn = self::_connect();
try{
$exec_str= $db_conn->prepare($sql_qry);
$exec_str->execute();
return $exec_str;
}catch(PDOException $e){
$log->save_to_log($e,__LINE__,__FILE__);
}
$db_conn = null;
}
和
$db= new database_class;
$sql_parties ="SELECT (SELECT political_party
FROM political_party_tbl
WHERE political_party_abbr = PARTY) as PARTY
,count_of_voters
FROM vrm_count_of_registered_voters_by_party_vw
ORDER BY PARTY";
$results = $db->db_fetch_dataset($sql_parties);
while($row= $results ->fetch(PDO::FETCH_ASSOC))
{
echo "<h4>".$row['PARTY'].": ".number_format($row['count_of_voters'])."</h4>\n";
}