Home arrow PHP arrow Page 2 - Object Interaction in PHP: Introduction to Aggregation, part 2

Fetching data with class: the “MySQLConnector” class - PHP

In this second part of his series, Alejandro Gervasio gets a little more technical with the basics of Aggregation. He begins working with a MySQL abstraction class and a useful paging class, and is starting to get into writing portable code and introducing the technique of aggregation.

  1. Object Interaction in PHP: Introduction to Aggregation, part 2
  2. Fetching data with class: the “MySQLConnector” class
  3. Adding Some Functionality to the Class
  4. Improving the “MySQLConnector class: adding row-counting methods
  5. Implementing the “MySQLConnector” class: a practical example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
June 01, 2005

print this article



Definitively, there is much to be gained from adopting an object oriented approach to use MySQL in different projects. While we find a plenty of room on the Web for well-structured MySQL abstraction classes, such as PEAR::DB to name a frequently used one, we’ll gain a practical grounding writing our own version, even if we’re quite far from getting that level of complexity.

Anyone who has spent a few days playing around with PHP, knows how to connect to MySQL and fetch some data, using a procedural approach. Yes, probably you’ve seen this code hundreds of times:

// connect to MySQL
$db=@mysql_connect('host','username','password') or die('Error connecting to the MySQL server');
// select database
@mysql_select_db('database',$db) or die('Error selecting database');
// perform a query selecting some records
$result=mysql_query('SELECT * FROM table',$db) or die('Error performing query');
// display results
// code to display results

In the above snippet we’ve connected to the MySQL server, selected a database, performing a simple “SELECT” statement to retrieve some records, finally displaying the results. All of the process was performed using the native PHP functions, taking a procedural approximation. I’m not going to complain loudly about these lines, since I’ve found myself, many times in the past, writing this code over and over again. However, if we’re honest, this approach doesn’t scale well for larger applications, even if we go one step further wrapping up the whole script into a function. A much better solution would be developing a class, which hides all of the internal processing to connect, fetch data, process results, and so forth, focusing primarily on the data handled, rather than on the functions themselves.

Keeping that idea in mind, let’s create a class to performs all of the most common operations associated to MySQL. This newly developed class structure, which I’ve denominated “MySQLConnector”, looks like this:

class MySQLConnector {
    // data member declaration
    var $conId; // connection identifier
    var $host; // MySQL host
    var $user; // MySQL username
    var $password; // MySQL password
    var $database; // MySQL database
    var $result; // result set
    function MySQLConnecto($host,$user,$password,$database){
        // code to setup  connection parameters
    // connect to MYSQL server and select database
    function connectDB(){
        // code to connect to the server and select database
    // perform query
    function performQuery($query){
        // code to perform query
    // fetch row
    function fetchRow(){
        // code to fetch a row

The above listed class, presents a few simple methods to deal with the usual tasks, often involved when working with MySQL. The constructor takes the same parameters used in the procedural solution, that is, the host to connect $host, the username $user, the password $password, and finally the database to work with, that is $database.

A quick look at the class shows us an immediate benefit, since we’re using one single method (the constructor) for connecting to the server and select the proper database. As you remember, the procedural solution takes up two lines of code.

The rest of the methods speaks for themselves. This way, we have a “connectDB()” private method that connect to the database, then a “performQuery()” method to perform a query against the database, and lastly, a “fetchRow()” method, which retrieves a row at a time from a result set. Definitively, the methods are very understandable, don’t you think so?

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: