0

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.

Rick Savoy
  • 321
  • 4
  • 11
  • At which line it's thrown? – Daniel Chernenkov Oct 02 '13 at 16:10
  • "while($row= ..." throws the error. – Rick Savoy Oct 02 '13 at 16:15
  • @RickSavoy That may be the line that's giving you trouble, however it could very well be the variable you've set to fetch. For example `$row['PARTY']` may need to be your actual field name. Another thing, how is this variable setup as `$sql_qry`? I only see 2 mentions of it, but no other code associated with it. Have a look at [this answer](http://stackoverflow.com/a/19017774/1415724) which may shed some light on the subject. – Funk Forty Niner Oct 02 '13 at 16:19
  • I suggest you further your research via Google using `fetch(PDO::FETCH_ASSOC) Fatal error: Call to a member function fetch() on a non-object` which produced many results. Good luck. – Funk Forty Niner Oct 02 '13 at 16:26
  • db_fetch_dataset is intended to be generic and receive any query string. In this case I am passing the contents of $sql_parties to the method. – Rick Savoy Oct 02 '13 at 16:27
  • what do you get with a print_r($results)? – Snowburnt Oct 02 '13 at 16:36

2 Answers2

1

Please correct the statement:

WHERE political_party_abbr] = PARTY) as PARTY

To:

WHERE political_party_abbr = PARTY) as PARTY
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

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";
}
Rick Savoy
  • 321
  • 4
  • 11