0

Hi,

I'm receiving an error message but cannot solve the problem. Please give us a hand. Line 49 is -> $record_set = $this->db_conn->query($sql);

Thanks in advance

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\oop_singleton_pattern\class.db.php on line 49

<?php
class DatabaseClass
{
    CONST HOST  = 'localhost';
    CONST USER  = 'root';
    CONST PWD   = '';
    CONST DB    = 'singleton';

    private $connection;
    private $database;
    public static $instance = null;


    public function __construct()
    {
        if (! @mysql_ping($this->connection))
        {
            $this->connection = @mysql_connect(self::HOST, self::USER, self::PWD);

            if ($this->connection)
            {
                $database = mysql_select_db(self::DB);

                if (! $database)
                {
                    echo 'Database connection error.';
                    exit;
                }
            }
            else
            {
                echo 'Server connection error.';
                exit;
            }
        }
    }

    public static function get_instance()
    {
        if (! isset(self::$instance))
        {
            self::$instance = new DatabaseClass();
        }

        return self::$instance;
    }

    public function select_all($sql)
    {
        $record_set = $this->connection->query($sql);
        return $record_set;
    }
}




$obj_db     = DatabaseClass::get_instance();
$record_set = $obj_db->select_all("SELECT * FROM users");

echo "<pre>";
print_r($record_set);
?>

Edited by veledrom

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by veledrom
0

mysql_connect returns a resource or false, not an object, so you cannot use the query function like that. Looks like you wanted to use mysqli to me.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.