Home arrow PHP arrow Page 2 - Generating Outputs from MySQL with Static Members and Methods in PHP 5

Working with MySQL - PHP

Trying to expand beyond the boundaries of your existing background by learning how to code and use static methods and properties inside your PHP 5 classes? Then look no further. Welcome to the last part of the series “Using static members and methods in PHP 5.” Made up of two instructive chapters, this series introduces the foundations of using static members and defines static methods in PHP 5-driven development environments.

  1. Generating Outputs from MySQL with Static Members and Methods in PHP 5
  2. Working with MySQL
  3. Creating the ResultToString, ResultToXML and ResultToArray classes
  4. Defining the ResultProcessorFactory class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 09, 2006

print this article



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

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: