0

我已经看过并且找不到我在这里做错了什么。我有一个使用以下方法创建的数据库类:

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()”

有人会好心地告诉我我做错了什么或指导我找到答案吗?谢谢!!

补充 - 仅供参考:查询本身不会导致错误。它在 SQL Server 管理器中运行良好。

4

2 回答 2

1

请更正声明:

WHERE political_party_abbr] = PARTY) as PARTY

至:

WHERE political_party_abbr = PARTY) as PARTY
于 2013-10-02T16:08:30.127 回答
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";
}
于 2013-10-02T17:05:54.460 回答