TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 08-23-2009, 02:17 PM   #1 (permalink)
The Visitor
 
Join Date: Aug 2009
Location: The Netherlands
Posts: 3
Thanks: 0
Nora is on a distinguished road
Help Accessing Mysqli From Different Class

I'm just getting started with OOP and I'm having trouble connecting to the database.

I made a class that would allowed to connect to the database but I had to use 'global $mysqli' in every method to avoid the non-object errors.
Now, I'm experimenting with a singleton pattern but I have no idea how to access the database connection from another class.

Database class:
PHP Code:
class database{

    private 
$mysqli;
    private function 
__construct(){
        
$this->mysqli = new mysqli($this->host$this->user$this->pass$this->name);        
    }

    public static function 
getInstance(){    
        if(
is_null(database::$instance)){
            
database::$instance = new database();
        }
        return 
database::$instance;    
    }

New class where it all goes wrong:
PHP Code:
class {
    public function 
select($sql){
        
$query $mysqli->query($sql);
    }

It works fine outside the classes using:
$db = database::getInstance();

I'd really appreciate it if someone could point me in the right direction.
Nora is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Accessing Variables from Conf file to Upload Class Gareth Absolute Beginners 10 02-22-2008 01:27 AM


All times are GMT. The time now is 05:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design