Home arrow PHP arrow Page 2 - Completing an Extensible Website Engine with PHP 5

Pulling web page contents from a database table - PHP

Are you one of those web developers searching for a comprehensive approach to constructing an expandable website engine with PHP 5? If your answer is a resounding yes, then this series might appeal to you. Welcome to the final part of the series “Building an extensible website engine with PHP 5.” In two tutorials, this series teaches you how to build a versatile website system which allows you to generate dynamic web documents by using a few simple PHP classes.

TABLE OF CONTENTS:
  1. Completing an Extensible Website Engine with PHP 5
  2. Pulling web page contents from a database table
  3. Injecting web page contents into the template file
  4. The website engine in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
October 31, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Due to the fact that the whole website engine will be fetching web page contents straight from the “pages” database table that was originally defined in the first article, the initial step involved in this project consists of creating a simple mechanism that allows for interaction with MySQL.

In response to the requirements mentioned above, I’ll define a couple of PHP classes tasked with handling all the processes related to connecting to MySQL and selecting databases, as well as executing SQL queries.

The respective signatures of these two MySQL processing classes are listed below:

// define ‘MySQL’ class
class MySQL{
    private $conId;
    private $host;
    private $user;
    private $password;
    private $database;
    private $result;
    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 fetchFormattedResult($query,$closeTag='</p>'){
        if(preg_match("/^SELECT/",$query)){
            throw new Exception('Query must begin with SELECT');
        }
        $output='';
        $opentag=str_replace('/','',$endTag);
        while($row=$this->fetchRow()){
            $output.=$openTag.$row.$closeTag;
        }
        unset($openTag,$closeTag);
        return $output;
    }
}

If you take some time and examine the source code of the above two classes, then you’ll possibly find them familiar, since I’ve been using them in some of my previous PHP articles.

On the other hand, if this doesn’t ring any bells to you, let me tell you that the first “MySQL” class is a simple wrapper. It's handy for connecting to the server, selecting a particular database, and running queries. The second one is responsible for performing a few useful tasks regarding the manipulation of result sets, like fetching and counting returned rows, and others.

At this point, I can say that the pair of MySQL processing classes that you saw previously are the first building blocks of the website engine that I’m currently building. They will handle all the processes required for fetching web page contents from the “pages” database table. Sounds logical, right?

Now, as you’ll probably recall, the entire presentation layer that corresponds to this website engine was completely handled by a single template file called “default_template.htm,” which also was created over the course of the first tutorial of the series. Keeping in mind this condition, it’s obvious that some kind of mechanism for processing that template is required here.

In order to tackle all the tasks for parsing the mentioned template file, in the following section I’ll be defining a useful template processing class with PHP 5. It will be charged first with taking up all the web page data pulled from the corresponding database table, and second with injecting this data straight into the respective template file.

After defining this template processing class, the PHP 5-based website engine that I originally planned to create will be near to completion, so hurry up and read the following section!



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