Home arrow PHP arrow Page 2 - Creating a Blog Application with Interpreter Classes with PHP 5

Working with MySQL - PHP

Working with interpreter classes in PHP 5 can be a useful experience for any PHP developer. Welcome to the final part of the series that began with “Building Interpreter Classes with PHP 5.” Composed of three educational tutorials, this series teaches you how to implement the interpreter pattern with PHP, by covering not only the corresponding theoretical concepts, but also showing you concrete cases where this pattern can be applied in a useful way.

TABLE OF CONTENTS:
  1. Creating a Blog Application with Interpreter Classes with PHP 5
  2. Working with MySQL
  3. Defining a basic blog interpreter class
  4. The interpreter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
April 16, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Definitely, a good point to start developing the blog application that I mentioned in the introduction is with defining a pair of MySQL processing classes. These will allow interaction with the database server in a seamless way. We're creating these classes because we want to have at our disposal a programmatic mechanism that facilitates the insertion, update and deletion of blog entries with minor hassles.

All right, now that you know why I decided to define these simple MySQL wrappers, take a look at their respective definitions. They're as follows:

// define 'MySQL' class
class MySQL{
   private $conId;
   private $host;
   private $user;
   private $password;
   private $database;
   private $result;
   const OPTIONS=4;
   public function __construct($options=array()){
     if(count($options)!=self::OPTIONS){
       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 MySQL
   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 ID form last-inserted row
   public function getInsertID(){
     if(!$id=mysql_insert_id($this->mysql->conId)){
       throw new Exception('Error getting ID');
     }
     return $id;
   }
   // seek row
   public function seekRow($row=0){
     if(!is_int($row)||$row<0){
       throw new Exception('Invalid result set offset');
     }
     if(!mysql_data_seek($this->result,$row)){
       throw new Exception('Error seeking data');
     }
   }
}

As you can see, the two MySQL processing classes listed above are indeed very easy to follow, not only because of their intrinsic definitions, but because I used them in some of my previous PHP tutorials, published here on the prestigious Developer Shed network.

The previous MySQL wrappers provide connectivity to the mentioned database server and perform some useful tasks with it, such as running queries, seeking and counting database rows, finding insertion IDs and so forth. As you'll see shortly, this group of database-related tasks will come in handy for processing diverse blog entries in a straightforward way.

So far, so good. After building the two MySQL processing classes that you learned a few lines above, it's time to take the next step toward the development of the blog application that I discussed in the beginning.

As you may have guessed, this step will consist of creating both a programmatic model of a typical blog entry via a simple PHP class, and building a blog interpreter which will be responsible for adding, updating and deleting these entries by implementing the homonymous pattern in conjunction with MySQL. Sounds fairly simple, right?

All these crucial topics will be covered in the following section, so keep reading to learn more about them.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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