PHP
  Home arrow PHP arrow Page 3 - Completing a Blogger with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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 a Blogger with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-12-05


    Table of Contents:
  • Completing a Blogger with PHP
  • A quick look at the BlogProcessor class
  • Providing the blog processor with database connectivity
  • Implementing client-side data validation on input forms
  • Improving the blogger’s visual appearance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 a Blogger with PHP - Providing the blog processor with database connectivity
    ( Page 3 of 5 )

    As I discussed in the previous section, the blog processor aggregates a simple MySQL wrapping class to have at its disposal the required capacity for handling all the database-related operations. As you’ll imagine, this functionality is easily achieved by coding a new pair of classes, which are shown below.

    Take a look at their respective definitions, please:

    // 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');

                            }

                }

    }

    All right, I think that the two classes listed above should be quite familiar to you, since I’ve been using them as part of different code samples in several PHP tutorials. With reference to these classes, you can see they offer a group of comprehensive methods for connecting to MySQL, running queries and handling result sets, which can be used inside the “BlogProcessor” class. Quite simple, right?

    Obviously this isn’t rocket science, so let’s move on to the next section. We'll learn how to provide all the input forms included with the blog processor with a basic JavaScript validation mechanism, which adds a useful feature to the whole application.

    To see how these client-side verification routines will be integrated with the blog application, please click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT