Working with MySQL Result Sets and the Decorator Pattern in PHP - Handling MySQL result sets (
Page 2 of 4 )
In order to demonstrate how the decorator pattern can be used for generating different outputs based on the same MySQL result set, first I’ll list two MySQL processing classes, which will be used for returning data sets from a sample database. So, having said that, below are the signatures of this pair of 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();
}
// connect to database
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');
}
}
// run query
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;
}
// fetch row
public function fetchRow(){
return mysql_fetch_assoc($this->result);
}
// count rows
public function countRows(){
if(!$rows=mysql_num_rows($this->result)){
throw new Exception('Error counting rows');
}
return $rows;
}
// count affected rows
public function countAffectedRows(){
if(!$rows=mysql_affected_rows($this->mysql->conId)){
throw new Exception('Error counting affected rows');
}
return $rows;
}
// get insert ID
public function getInsertID(){
if(!$id=mysql_insert_id($this->mysql->conId)){
throw new Exception('Error getting ID');
}
return $id;
}
// seek rows
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');
}
}
// return result set
public function getResult(){
return $this->result;
}
}
The two classes listed above shouldn’t be difficult to understand at all, since I used them as examples in previous PHP-related tutorials. In simple terms, these classes allow you to connect to MySQL and return result sets from a selected database, which is very convenient for my purpose of demonstrating how some decorator classes can process a specific result set.
So far, so good. At this stage, I have provided you with a couple of straightforward MySQL processing classes. Now, it’s time to pay strong attention to the next few lines, where I’ll be developing some decorator classes to generate different outputs from a given MySQL data set.
Want to see how this will be done? Then please read the following section.