PHP
  Home arrow PHP arrow Page 2 - Completing an Extensible Website Engin...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Completing an Extensible Website Engine with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2006-10-31

    Table of Contents:
  • Completing an Extensible Website Engine with PHP 5
  • Pulling web page contents from a database table
  • Injecting web page contents into the template file
  • The website engine in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Completing an Extensible Website Engine with PHP 5 - Pulling web page contents from a database table


    (Page 2 of 4 )

    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


       · The final part of the series demonstrates how to create a fully-functional website...
       · I tried to create an account here but I never receive my password...so-- I'm a...
       · Thank you for posting your comments on my PHP article, and I'm glad to know you're...
       · I've been playing with this for a while and it's outputting a blank page, view...
       · Hello there,Thank you for posting your feedback here. Now, concerning your...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT