OO PHP Part 1: Re-usable DB connector

Welcome to My first Tutorial and resource for OO PHP code and classes. There are a lot of Great OOP Tutorials out there for PHP. This tutorial is not for people who don’t have the OOP concept down, this tutorial is for the person that understands the concept and just needs some great reusable code for your library of scripts and Code to make your life a little bit easier.

First we need to start with the DB connector so we can connect to the database, hopefully if your reading this you most likely have a MySQL DB either setup or have access to, if you have another Database setup the code can be modified to be used with it as well but that goes beyond the scope of this tutorial. Next you will have to Have PHP installed and setup, also not going to be covered in this tutorial.

now for the the Code, i will explain each part and what its Purpose is.

First we need to create our class

class SystemComponent {} this will create our class, now we need to add the rest of the to finish this part off, now we add your variable var $settings;
this will save our DB settings will be saved and recalled later,as a side point most tutorials i have read or stumbled upon use multiple variables to store DB settings, that will work but i found that using an array works just as well but for this class we will use the array. Now we need to Create the function that will return our DB settings with out exposing the DB login to other People.
function getSettings() {}
this function will return our variable when finished,
next we add our data

// Database variables
$settings['dbhost'] = 'localhost'; //hostname
$settings['dbusername'] = 'root'; // DB user login
$settings['dbpassword'] = 'password'; // password
$settings['dbname'] = 'DBname'; //database to be used

next we need to return the info when the function is called, we use return $settings; To be more technical it would be considered a method not a function since it is in a class.
Now lets put the Whole thing together and call it dbconfig.php or config.php

class SystemComponent {
var $settings;
function getSettings() {
// Database variables
$settings['dbhost'] = 'localhost'; //hostname
$settings['dbusername'] = 'root'; // DB user login
$settings['dbpassword'] = 'flipsql'; // password
$settings['dbname'] = 'geodata'; //database to be used
return $settings;

The best practices that i have learned is have this file located in a protected directory that cant be viewed, but this is only a suggestion not a rule. This Format can be used for other application settings or another DB connection if you need different DB log in, the possibilities are endless.

Now for the Second half of the this connector, For me i created another file called “libconnect.php”. The way that i have structured my code, is in another folder called ‘lib’
to give an example of how its set up

  • /   ‘root folder’
  • /application/   ‘application folder, holds most of the application files’
  • /application/lib/ ‘library folder, contains Library files that are going used by the app’
  • /application/lib/config/ ‘configuration folder, holds basic or advanced configuration for your site’

so after we create libconnect.php in the lib folder we start by including a page into this file by calling include_once
include_once $_SERVER['DOCUMENT_ROOT'] . '/application/lib/config/config.php';
Depending where the file is you could use require function instead i leave it up to you on how you want to call the file. next we create the Class but this time we are going to extend another class which i found very helpful when needing variables set in other classes but we will go into that another time. As before we creat a Class and extend the system component class, (this will allow access to the variables, class DbConnector extends SystemComponent {} then we create the class varibles that are needed for the methods to work correctly, we create var $theQuery; to hold the query code, and now we need a Link reference var $link; this will hold the DB connection info so it doesnt have to open another DB connection when called within the same object.
Now onto the Function

function DbConnector(){
$settings = SystemComponent::getSettings();
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
$this->link = mysql_connect($host, $user, $pass);
register_shutdown_function(array(&$this, 'close'));}

there may be some lines of code you don’t understand, like register_shutdown_function() which is a function that is built in, you can find the specifics here, the reason i have it is you don’t have to Close the DB link when the script finishes, it will automatically close it when the script is finished so you do not have to call close when your done. Now we need to add the Close function so the method works correctly and doesn’t throw up any errors
function close() {

Now that we have a Good start to our application now we can right the code that will connect to the DB and show you how it works
some sample code

run this if all went well you should now have a link to the DB to do with as you please,
the next addition will add some more usefull methods to add to dbconnector.php, like Querying, returning last row modified and so on and also creating a safe way to have user added data from a form that is safe from SQL injections.
Thanks for Stopping by