HomePHP 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.
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()){ if(count($options)<4){ throw new Exception('Invalid number of connection parameters'); } foreach($options as $parameter=>$value){ if(!$value){ throw new Exception('Invalid parameter '.$parameter); } $this->{$parameter}=$value; } $this->connectDB(); } private function connectDB(){ if(!$this->conId=mysql_connect($this->host,$this- >user,$this->password)){ throw new Exception('Error connecting to the server'); } if(!mysql_select_db($this->database,$this->conId)){ throw new Exception('Error selecting database'); } } public function query($query){ if(!$this->result=mysql_query($query,$this->conId)){ throw new Exception('Error performing query '.$query); } return new Result($this,$this->result); } } // define 'Result' class class Result { private $mysql; private $result; public function __construct(&$mysql,$result){ $this->mysql=&$mysql; $this->result=$result; } public function fetchRow(){ return mysql_fetch_assoc($this->result); } public function countRows(){ if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); } return $rows; } public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected rows'); } return $rows; } public function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } public function seekRow($row=0){ if(!int($row)||$row<0){ throw new Exception('Invalid result set offset'); } if(!mysql_data_seek($this->result,$row)){ 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.