PHP MySQL Class

So it’s probably pretty late in “the game” to be posting this, however I thought I might do it anyway just for those that may want to get started using OOP in PHP. I’ve been using this for a while and improving it wherever I can, then I started using CodeIgniter which ended up having a very similar class built into it. So if you are wanting to learn OOP, this might help you a long and it will make for a good transition into an MVC if you decide to move forward from there.

Keep in mind this has very simple functionality. For the most part, you can execute any style of query you want just by using the first example, however, there are a few extra methods built in that will allow you to easily do inserts and updates.

Call me crazy but I never really got around to adding a delete method in here, mainly because I RARELY delete data from a database, I usually update the row with a “delete date” in some sort of delete column. I can’t imagine adding delete functionality would be very hard. If you decide to use this, tell me what you think!

Usage:
Basic Select

Select with Where

Insert

Update

Please note, my recent updates include adding a new function “seek” and now default to return a mysql_fetch_object() instead of mysql_fetch_array(). This can be easily changed back in the constructor.

2 Responses

  1. Joe Coolest

    I’m attempting to use this class to bridge the gaps in my OOP PHP knowledge. OOP is quite a challenge for me at the moment.

    For the life of me I can’t figure out how to implement a mysql_data_seek function into your class.

    function seek($index) { return mysql_data_seek( $this->????, $index ); }

    I thought I could easily extend your class with a few features to allow me to seek to first and last result of a query, but I am completely stumped at this point. I’m not giving up and don’t really want the functions handed over, but I’d love a few pointers that might help me fill in the gaps in my knowledge regarding what I would need to pass to mysql_data_seek.

    I look forward to becoming a regular follower of your blog. A few of the posts that I have investigated thus far have been extremely helpful. Thank you.

Write a Comment