PHP
  Home arrow PHP arrow Page 2 - Generating Outputs from MySQL with Sta...
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

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-10-09

    Table of Contents:
  • Generating Outputs from MySQL with Static Members and Methods in PHP 5
  • Working with MySQL
  • Creating the ResultToString, ResultToXML and ResultToArray classes
  • Defining the ResultProcessorFactory class

  • 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


    Generating Outputs from MySQL with Static Members and Methods in PHP 5 - Working with MySQL


    (Page 2 of 4 )

    Before I proceed to demonstrate how to use static methods for generating different outputs from a given MySQL database table, first I need to create a pair of classes that allow me to interact with the database server. Taking this into account, I listed the signature for these two classes 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; } public function getResultSet(){ return $this->result; } }

     

    In simple words, the above pair of classes will allow me to perform some useful tasks, like connecting to MySQL, running queries, handling data sets, and so forth, which is very convenient for generating different outputs from one or many database tables. However, definitely these classes aren’t the main subject of this article, therefore I don’t want you to get bored quickly reading my explanations about what they do.

    Instead, now that the classes have been defined, let’s move on to the following section and learn more on how to create a few more understandable classes, where each of them will be responsible for generating different outputs from some MySQL database tables. And don’t you worry, because static methods are just around the corner, trust me!

    In order to see how these brand new classes will be defined, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · This final article of the series is pointed out to demonstrate how to use static...
     

       

    PHP ARTICLES

    - 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
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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