Call to a member function query() on a non-object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emekachibuzo
Forum Newbie
Posts: 1
Joined: Sat Oct 12, 2013 8:53 am

Call to a member function query() on a non-object

Post by emekachibuzo »

Hi. I'm a beginner in PHP. I'm trying to get data from database but I'm having the following error
"Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\phpExamPrep\studentDB.php on line 22"
I'm sorry I have to include all the codes, its just to help you help me. Thanks
I have the following classes

Code: Select all

class database {
 private static $dsn = 'mysql:local=localhost;dbname=test1';
 private static $username ='me';
 private static $password ='me';
 public static $db;
 
 private function __construct() {
     
 }

 public static function  dataConnect()
    {
        if(isset(self::$db))
        {
            try
            {
                self::$db = new PDO(self::$dsn, self::$username, self::$password);
                
            }
            catch(PDOException $e)
            {
                $error_message = $e->getMessage();
                include 'database_error.php';
                exit();
            }
        }
        return self::$db;
    }
}


class studentDB {
   
    
   public static function getAllRecords()
    {
       require  'database.php';
        $query = 'select * from student';
        
        $db = database::dataConnect();
        $result = $db->query($query); //This part is giving error
        $students = array();
        
        foreach($result as $row)
        {
            $stud = new student($row['firstname'],$row['lastname']);
            $stud->setID($row['id']);
            $students[]=$stud;
        }
        
        return $students;
    }
}

<?php
            include 'studentDB.php';
            $stx =studentDB::getAllRecords();
            
            foreach ($stx as $r)
            {
                echo $r->getFirstName().' '.$r->getLastName().' '.$r->getID();
            }
        ?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Call to a member function query() on a non-object

Post by Christopher »

Code: Select all

        $db = database::dataConnect();
        $result = $db->query($query); //This part is giving error
If $db is not an object then dataConnect() is not returning an object.
(#10850)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Call to a member function query() on a non-object

Post by AbraCadaver »

Maybe NOT isset():

Code: Select all

public static function  dataConnect()
{
    if(!isset(self::$db))
    {
            try
            {
                self::$db = new PDO(self::$dsn, self::$username, self::$password);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply