Building A PHP Database Connection Class From Scratch (using Singleton & ActiveRecord patterns)

EH_DESIGN_2012.04.03_PhpMySQLLogo_07Greetings folks.

Tonight I’m studying on how to create a PHP Object-Oriented MySQL database connection class.  This will be reusable and able to be implemented in any database driven web application built with PHP & MySQL.  It uses the ActiveRecord and Singleton design patterns.

I’m currently following this YouTube tutorial: Using OOP PHP to select, update, and insert data in a mysql database. Nick Huot really does a superb job of explaining step-by-step, not only what he is doing, but also why.

(24 hrs later …)

So I am pleased to tell you, dear reader, about the successful PHP OOP Database Connection class I built last night, based on Huot’s tutorials — they really are the best I’ve seen.

I had a lot of debugging to do, but that was simple.  Nick had not provided all the code for PHP validating of variables and such.  I’m sure he would have but he probably ran short on time, so no worries.  The validations I came up with (making sure rows had values and were of type array, for instance), I have provided for you below in hopes it will save some of you some time.  If you choose to implement my code examples you may find that there are still validations I had not accounted for.  I’d love to hear of any bugs that you find, or improvements that you make on this example, or on your own projects derived from this code.

COMPLETE WORKING EXAMPLE:

config.php

database.class.php

table.class.php

user.class.php

index.php

Now in the interest of time, I’m publishing this post.  Soon I will add commentary and explanations and such, however, experienced Object-Oriented PHP developers will understand how to code fits together without explantion.  The best I can do for the newbies for now is to say if your database exists, has data matching the format the class is looking for , and is running you should be able to put all these files in your PHP Apache web directory (I’m using WAMP and on my system it is in C:wampwww; on yours it may be different), and launch index.php to see a first and last name pulled from one row of data, specified by id number, in your MySQL database.

Good luck, and I’d love to hear from you if you have questions, comments, or issues that you ran into.

– CodeSlayer2010

P.S.: Go ahead and comment out the requires and includes for “viewutilities.php”. If you don’t, the code will not work because it will complain that it can’t find this file.

I left it in there because I will be publishing it later on and you may find it useful debugging tool for your PHP scripts. This class is a wrapper for print_r() function and there are all kinds of advantages, the least of which is one line of code vs three with all your pre-tag. But this class and it’s static “debug” method streamline and simplify debugging complex AMP-stack PHP-based web applications. Watch for it soon!

410total visits,6visits today

Tagged , , , , , , , , , , , , , ,

11
Leave a Reply

avatar
7 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
FlemmingActive record design pattern[PHP/MySQL] | BlogoSferaBinding variables to a superclass in PHP | BlogoSferaActive record design pattern[PHP/MySQL] - How-To Videoahmed Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
ahernilesh74
Guest

its good sample script… i like it…!

kristobaldude
Guest
kristobaldude

Thanks — glad you liked it! Make sure to email subscribe to keep up to date on all my new posts. I’ve got a backlog of about 20 tech posts that I’ve still to publish. 🙂

kira
Guest
kira

Thanks for this. 🙂

kristobaldude
Guest
kristobaldude

YW

ahmed
Guest
ahmed

Good job …

kristobaldude
Guest
kristobaldude

thank you

trackback

[…] codeslayer2010.wordpress.com/2… […]

Flemming
Guest

at line 48 of the database.class.php, u have this ‘$this->results->num_rows’, it need to be this i guess : ‘$this->results->num_rows()’, also on line 54.

Let me know if it works ???

kristobaldude
Guest
kristobaldude

@Flemming – Thanks for the correction! I won’t be able to test it any time soon, but yes, you are correct — num_rows is a function so it should definitely have the parentheses on the end.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close