PHP
  Home arrow PHP arrow Page 2 - Generating Outputs from MySQL with Static Members and Methods in PHP 5
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? 
Google.com  
PHP

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek