The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Am I inscure?

Im building a base class for my website that is going to contain all the basic information for my site, like the header, footer, mysql connection (which is going to be in a separate class).

The mysql connection is what I'm unsure about, If I set the connection variables (ie. mysql username, password, host, etc) to private and made the actual connector ($connect = mysql_connect(xxxxx)) public, would that be secure of not? It's just that its getting a bit tedious having to make mysql connections for each class (i mean having to call my mysql class).

To put it short, I want to make a connection to my mysql server in the class "base" and for all the classes that extend the "base" class to be be able to access the connection without having to make a new one, would setting the variables to private and the connector to public be secure or not?

When i use: $settings_query = mysqli_query( $this->_oDatabase , "SELECT * FROM `"._settingstbl."");
I get: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\classes\class.base.php on line 22

And,
When I use: $get_settings = $this->_oDatabase->query("SELECT * FROM `"._settingstbl."`");
I get: Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\classes\class.base.php on line 21

When I use: $get_settings = $this->_oDatabase->query("SELECT * FROM `"._settingstbl."`");
I get: Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\classes\class.base.php on line 21

Now this means that the $_oDatabase property is not a properly instantiated object. In his example, SilverBulletUK used $oDatabase (without the underscore). Make sure you're using the same property names.

protected $oDatabase;// Database connection
public $_filebase; // The absolute path to the website on the server.
public $_urlbase; // The base url of the website.
public $_config; // Location of the config.

In Database constructor check if the object has been created before returning it. If it hasn't been created then check for errors. You're probably passing wrong database details to the function (host, username, password).

I'm not entirely sure if the mysqli object throws an exception when it doesn't connect, so I just added a little routine.

Originally Posted by decowski

In Database constructor check if the object has been created before returning it. If it hasn't been created then check for errors. You're probably passing wrong database details to the function (host, username, password).

Also at the top of your script add:

PHP Code:

error_reporting(E_ALL | E_STRICT);

After implementing both of your suggestions i get the following error:
Fatal error: Call to undefined method Database::query() in C:\xampp\htdocs\classes\class.base.php on line 86