Close Search Box
Search Box

Search: From:

Close
Newsletter

9Tutorials to your Inbox



Creating an object oriented MySQL abstraction class

Creating an object oriented MySQL abstraction class
Author lv1 (2000/5000)
2,388 views
1 Star2 Star3Star4 Star5 Star (5 votes, average: 3 out of 5)

In this tutorial I will show you how you can create an object oriented database abstraction class for MySQL database. Besides the basic functions this manager class can summarize total time spent with db connection and query execution and counts how many query were executed.
Step 1.
The first question is why we need at all such script if we have all function in PHP as default. There are more arguments:

  • You can easy reuse the script in all of your development.
  • You can change the database without changing all of your script. You need to edit only the abstraction class.
  • You can make optimalisation and simplification more easy.
  • You can centralize error handling and db access.
  • You can make the debugging easier.
  • You can easy implement query time statistics and query counters.

Now let’s summarize what kind of function do we need:

  • Creating a connection to the database
  • Executing sql queries
  • Get the number of selected rows if possible
  • Get the number of modified rows if possible
  • Get the auto increment id of the latest insert statement if possible
  • Load a query result into a normal array
  • Get the error message and/or id if necessary
  • Get the total time spent in the database
  • Get the number of the executed queries

Step 2.
Before we can start the implementation of the functions we need to collect all common used class variables which stores important information.
These are:

  • A connection identifier to store the actual connection link.
  • A result Set to store query results.
  • An error message and error id to store error information.
  • A counter to count executed queries.
  • A time counter to store total query times.

So the basic DB manager class looks like this:

Step 3.
Now we can implement the functions. Let’s start with the most important one. Connecting to the database. We will create no stand alone connect script but the constructor of the class will be responsible to build up the database connection. So if you create a database object from this class you immediately have a valid connection as well.

So the constructor needs the following parameters:

  • hostname
  • username
  • password
  • database name

In the function first we try to connect to the database server and if it was success than we try to select the given database. In case of any error we set the error message and error id variables and return with false.

The constructor of the class is the following:

Step 4.
As next step we will implement the query execution function. In this function we measure the execution time (see details later), increasing the query counter and of course executing the query itself. If an error occurs than we set the error message and error id variables and returns with false. If everything was working fine we return with the result set.

The code is the following:

If the query contains an Insert, Update or Delete command than it can be important to know how many records were affected by this query. To get it we implement a function for that as follows:

In case of a Select statement the number of selected records could be interesting. To get it we have a similar function:

In this topic we have a last important issue. In case of Insert maybe we want to know an auto increment filed value. To get this information after the record was inserted we can use the following code:

Step 5.
Now let’s create some other useful function to make our class more usable.
In case of a select it sometimes it would be nice to get an associative array instead of the MySQL result set. So we implement a function which returns with such an array generated from the actual result set.
The code looks like this:

As our function don’t send error messages back to the caller just sets the error variables inside the class so we need function to get this information. These are simple getter functions as follows:

Step 6.
As final step we need to implement a function to get query times. To do it we need a function which returns the actual time in milliseconds. With calling this function at the beginning and at the end of the affected db functions we can calculate the total db time. Besides this we implement 2 other getter functions to get the number of executed queries and the total query time in a usable format.

So our helper functions looks like this:

Copyright @ PHP Toys 2007

del.icio.us:Creating an object oriented MySQL abstraction class digg:Creating an object oriented MySQL abstraction class spurl:Creating an object oriented MySQL abstraction class newsvine:Creating an object oriented MySQL abstraction class blinklist:Creating an object oriented MySQL abstraction class furl:Creating an object oriented MySQL abstraction class reddit:Creating an object oriented MySQL abstraction class blogmarks:Creating an object oriented MySQL abstraction class Y!:Creating an object oriented MySQL abstraction class magnolia:Creating an object oriented MySQL abstraction class segnalo:Creating an object oriented MySQL abstraction class
Login




Friends' Sites

Contact Us

Categories

Sponsors

Featured Links

Post a Comment »








Safari hates me

Comment Guidelines

  • Hyperlinks are automatically generated.
  • <em>italic</em>
  • <strong>bold</strong>
  1. Jarek June 2, 2007

    Something is wrong with code-listing ?

  2. Araba June 8, 2007

    the great article

  3. Eko September 8, 2007

    are you sure about that code-listing?

  4. atc September 28, 2007

    Something is wrong with code-listing ? http://www.w3answers.com

  5. Sunil September 30, 2007

    Hi, I am novice in object oriented programming in PHP.

    Can you please let me know why are you using this much of Spans etc……..,

    Sunil