Page 1 of 1

php5 class with mysqli connection

Posted: Sat Apr 11, 2009 5:43 am
by pl_towers
Hi
I have started learning php5 and I have hit an annoying problem which i just cant understand I am sure its simple but after googling for the last couple of days i still havent found the answer.

The thing is I want one class which connects to the db and all my other classes use that class to connect to the db.

What I have is:

Code: Select all

 
class DB {
    
    private $connection;
    
    private static $_instance;
    
    
    private function __construct() {
        
        $this->connection = new mysqli('localhost', 'user', 'pass','database');
 
    }
 
    public static function get_connection() {
        
        if(is_null(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;    
    }
    
}
 

and my test class I am trying to get to call and use the db class is:

Code: Select all

 
class mine {
    
    private static $mysqli;
    
    public function __construct () {
        
        self::$mysqli = DB::get_connection();
    }
    
    public function test(){
        $sql = "SELECT * FROM table";
        $result = self::$mysqli->query($sql);
        
        while ($test = self::$mysqli->fetch_assoc($result)) {
            
            echo $test['email'] . "<br />";
        }
    }
}
 
But i get the "Call to undefined method DB::query()" which is true because my db has no query function, think I am going at this the wrong way, just want a db connection i can use in all my classes.

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 6:15 am
by requinix
Do you want to use functions in DB or are you trying to use the mysqli class?

DB::_instance is a DB, not a mysqli.
mine::mysqli is also a DB, not a mysqli.

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 6:28 am
by pl_towers
yes what i want is to use the mysqli within my class, but i have used $mysqli->query() in my class but that doesnt work, I am not sure how i can use the oop $mysqli within a class.

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 7:05 am
by requinix
Like I said, mine::mysqli is a DB object. Now spend a minute thinking about that.

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 8:23 am
by pl_towers
Okay i think i get it, i was getting an object of the DB not the mysqli.

I think i have got it working, I have changed my DB class to:

Code: Select all

 
class DB {
        
    private static $_instance;
    
    private function __construct() {
        
    }
 
    public static function get_connection() {
        
        if(is_null(self::$_instance)) {
            self::$_instance = new mysqli('localhost', 'user', 'pass','database');
        }
        return self::$_instance;    
    }
    
}
 
this now returns a object of the mysqli

and my test class now looks like this:

Code: Select all

 
class mine {
    
    private static $mysqli;
    
    
    public function __construct () {
        
        self::$mysqli = DB::get_connection();
        
    }
    
    
    public function test(){
        
        $sql = "SELECT * FROM test";
        
        if($this->$mysqli->real_query($sql)) {
            if($result = $this->$mysqli->store_result()){
                
                while($test = $result->fetch_assoc()) {
                    echo $test['email'] . "<br />";
                }
                $result->close();
            }
        }
    }
}
 
this is actually working at last! is this the best way to be doing it all?

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 8:35 am
by requinix
"Best" isn't the word. Not that it's wrong or anything, it's just one of the ways you can do this.

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 8:56 am
by pl_towers
what are the other ways of doing this then?

Re: php5 class with mysqli connection

Posted: Sat Apr 11, 2009 9:10 am
by requinix
Hmm...
  • Singleton that serves a mysqli (what you have now)
  • Singleton that wraps a mysqli (you use DB->func, not DB::mysqli->func)
  • Global variable (not good practice)
  • New mysqli objects every time you need them. Probably reuses connections like the mysql extension does
Probably a couple other but those are the popular ones.