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.

6
  • At which line it's thrown? Commented Oct 2, 2013 at 16:10
  • "while($row= ..." throws the error. Commented Oct 2, 2013 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 which may shed some light on the subject. Commented Oct 2, 2013 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. Commented Oct 2, 2013 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. Commented Oct 2, 2013 at 16:27

2 Answers 2

1

Please correct the statement:

WHERE political_party_abbr] = PARTY) as PARTY

To:

WHERE political_party_abbr = PARTY) as PARTY
Sign up to request clarification or add additional context in comments.

1 Comment

Corrected- it was just a typo here. Thanks
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";
}

2 Comments

what did you add/delete/change?
The changes I made were in the db_fetch_dataset method. You can compare the two. Also I was calling my save_to_log method incorrectly- buit this did not cause the other problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.