Home arrow PHP arrow Page 2 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP

The starting point of a brand new MVC schema: defining some MySQL processing classes - PHP

Are you a curious PHP developer, wanting to learn how to simulate a basic Model-View-Controller schema with PHP? Then, look no further, because you’re in the right place. Welcome to the final part of the series “Simulating the Model-View-Controller Schema in PHP.” In three tutorials, this series provides you with a comprehensive introduction, aimed at implementing a MVC-based relationship between PHP objects.

  1. Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
  2. The starting point of a brand new MVC schema: defining some MySQL processing classes
  3. Setting up the basics for generating disparate views: defining a controller based on MySQL datasets
  4. Defining the next link of the chain: creating a model founded on native MySQL data sets
  5. Generating distinct views from a single MySQL result set: creating an output generator class
  6. Putting the classes to work together: seeing the MVC schema in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
August 21, 2006

print this article



Since my intention here is to demonstrate how to generate several views from a single MySQL result set, logically the first step involved in developing this example consists of defining a couple of MySQL-related PHP classes. These classes can be used first for connecting to the database server, and then for obtaining the data sets that I mentioned before.

That said, here are the corresponding signatures for these classes:

// define 'MySQL' class
class MySQL{
    private $conId;
    private $host;
    private $user;
    private $password;
    private $database;
    public function __construct($options=array()){
            throw new Exception('Invalid number of connection
        foreach($options as $parameter=>$value){
                throw new Exception('Invalid parameter
    private function connectDB(){
            throw new Exception('Error connecting to the
            throw new Exception('Error selecting database');
    public function query($query){
            throw new Exception('Error performing query
        return new Result($this,$this->result);
// define 'Result' class
class Result {
    private $mysql;
    private $result;
    public function __construct(&$mysql,$result){
    public function fetchRow(){
        return mysql_fetch_assoc($this->result);
    public function countRows(){
            throw new Exception('Error counting rows');
        return $rows;
    public function countAffectedRows(){
            throw new Exception('Error counting affected rows');
        return $rows;
    public function getInsertID(){
            throw new Exception('Error getting ID');
        return $id;
    public function seekRow($row=0){
            throw new Exception('Invalid result set offset');
            throw new Exception('Error seeking data');
    public function getResultSet(){
        return $this->result;

Certainly the above classes should be quite familiar to you, since I've been using them in several cases over the course of different PHP tutorials that were published previously on the prestigious Developer Shed network. However, as I mentioned before, I need to have at my disposal a pair of simple MySQL processing classes to work with, in order to start defining a consistent MVC schema.

Now, let's get rid of the boring stuff, and concentrate on something much more interesting. Once the previous classes have been defined, it's time to learn how to construct a brand new controller class. This class should be able to instruct the model (in this case represented by a MySQL data set) to generate different outputs or views from it. That sounds attractive indeed! Thus, jump straight into the upcoming section and learn how it will be done.

>>> 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: