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.

  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



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()){
            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 fetchFormattedResult($query,$closeTag='</p>'){
            throw new Exception('Query must begin with SELECT');
        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


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