Reputation: 341
I have looked and can not find what I am doing wrong here. I have a database class I created with the following method:
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;
}
I am using it here:
$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";
}
I am getting the following error: "Fatal error: Call to a member function fetch() on a non-object"
Would someone be kind enough to tell me what I am doing wrong or direct me to the answer? Thanks!!
Added- fyi: the query itself is not causing the error. It runs fine in SQL Server Manager.
Upvotes: 0
Views: 59
Reputation: 341
Ok, got it working. Here is what worked:
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;
}
and
$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";
}
Upvotes: 0
Reputation: 23978
Please correct the statement:
WHERE political_party_abbr] = PARTY) as PARTY
To:
WHERE political_party_abbr = PARTY) as PARTY
Upvotes: 1