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);
?>

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.

My error. Line should read

$record_set = mysql_query($sql);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.