Home arrow MySQL arrow Page 3 - Abstracting Access to MySQL for an MVC-Based Framework in PHP 5

Building a basic database abstraction class - MySQL

In this third part of the series, I further enhance the functionality of this sample MVC framework by adding a class to it that takes care of interacting with MySQL in a simple manner. You’ll see the importance of this abstraction class when I show you the definition of the corresponding model, which will aggregate an instance of “MySQL” to handle the data layer very easily.

TABLE OF CONTENTS:
  1. Abstracting Access to MySQL for an MVC-Based Framework in PHP 5
  2. Listing the partial source files of the framework
  3. Building a basic database abstraction class
  4. Showing the framework’s source files
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
June 09, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you may know, any modern MVC-driven framework must be at least capable of using a RDBMS for storing data, and the one that I’m building here won’t be the exception. For this particular project, there will be a model class that will handle specifically the data layer, but this model will use an aggregated abstraction class to interact directly with MySQL.

Based on these requirements, it’s necessary to build that abstraction class, so below I coded one that performs quite decently. Look at it, please:   

<?php

class MySQL

{

                private $result = NULL;

                private $link = NULL;

                private static $instance = NULL;

                // return Singleton instance of MySQL class

                public static function getInstance(array $config = array())

                {

                                if (self::$instance === NULL)

                                {

                                                self::$instance = new self($config);

                                }

                                return self::$instance;

                }

               

                // constructor

                public function __construct(array $config = array())

                {

        // grab connection parameters

                                list($host, $user, $password, $database) = $config;

                                if ((!$this->link = mysqli_connect($host, $user, $password, $database)))

                                {

                                                throw new Exception('Error connecting to MySQL : ' . mysqli_connect_error());

                                }

                }

    // perform query

    public function query($query)

    {

        if (is_string($query) and !empty($query))

        {

            if ((!$this->result = mysqli_query($this->link, $query)))

            {

                throw new Exception('Error performing query ' . $query . ' Message : ' . mysqli_error($this->link));

            }

        }

    }

   

                // fetch row from result set

                public function fetch()

                {

        if ((!$row = mysqli_fetch_object($this->result)))

        {

            mysqli_free_result($this->result);

            return FALSE;

        }

        return $row;

                }

    // get insertion ID

    public function getInsertID()

    {

        if ($this->result !== NULL)

        {

            return mysqli_insert_id($this->link);

        }

        return FALSE;

    }

   

    // count rows in result set

    public function countRows()

    {

        if ($this->result !== NULL)

        {

           return mysqli_num_rows($this->result);

        }

        return 0;

    }

   

                // close the database connection

                function __destruct()

                {

                                is_resource($this->link) and mysqli_close($this->link);

                }

}// End MySQL class

I'm not trying to market to you the above “MySQL” class, because that would be a waste of time. However, it’s fair to say that it works pretty well; it implements a few straightforward methods for running queries, fetching and counting rows in datasets, and finding insertion IDs. That's not too bad, considering that this is only an example.

Regardless of the logic implemented by this MySQL abstraction class, which can be more or less complex, the most important thing to note here is that it provides the functionality required by the framework to access database tables via a simple and intuitive API. The crucial relevance of this class will become really evident when I start developing the framework’s model, but for the moment it should be analyzed as a standalone piece of code.

Now that the framework has acquired the ability to work with MySQL by means of the “MySQL” class that you saw before, it’s time to list its full source code, so you can see the progress that has been achieved so far.

This will be done in the last section of this tutorial, so click on the link that appears below and read the next few lines.



 
 
>>> More MySQL Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: