Tutorial details
Name : Database Statement
Author : Kenneth Clark
Language : PHP 5
Platform : Platform Independant
Click here if you require development or hosting solutions : [ SkyeTech Solutions ]

Once you have created your Database Connection class you are going to be required to create the statement class.

Right let us begin. Firstly please note that unless you have compeleted the database connection class you will not be able to complete this tutorial.

The statement class is the wrapper that will contain the functions for handling result sets.

class DB_MySQLStatement{
  public      $result;
  public      $query;
  protected   $dbh;
  public      $binds;

  public function __construct($dbh, $query){

    $this->query    = $query;
    $this->dbh      = $dbh;
    if(!is_resource($dbh)){
      throw new MySQLException("Not a valid
                                database connection");
    }
  }
}
This is the base of the class containing the contructor. All of it is pretty self explanitory.

Now for the methods. The first method we will add is the execute method

public function execute(){
  $binds  = func_get_args();
  foreach($binds as $ph => $pv){
    $this->query =
      str_replace(":$ph","'" .
                  mysql_escape_string($pv) .
                  "'",$this->query);
  }
  $this->result =
    mysql_query($this->query,$this->dbh);
  if(!$this->result){
    throw new MySQLException;
  }
  return $this;
}
This function effectively prepares and executes the sql statment. $binds contains the arguments that are to be placed inside the SQL query.

Right the next functions to add are the fetch functions. These will return the result set.

public function fetchRow(){
  if (!$this->result) {
    throw new
      MySQLException("Query not executed");
  }
  return mysql_fetch_row($this->result);
}

public function fetchAssoc(){
  return
    mysql_fetch_assoc($this->result);
}

public function fetchAll(){
  $retval = array();
  while($row = $this->fetchAssoc()){
    $retval[] = $row;
  }
  return $retval;
}
Then finally we create the Exception class used when a MySQLException is thrown. This is a very simple class.
class MySQLException extends Exception{
  public $backtrace;
  public function __construct($message = false,
                              $code = false){
    if(!$message){
      $this->message = mysql_error();
    }
    if (!$code) {
      $this->code    = mysql_errno();
    }
    $this->backtrace  = debug_backtrace();
  }
}

Then finally our connection factory, this is effectively a function that will return a database object.

function DB_Connection_Factory($key){
  switch($key){
    case "default":
      return new Default_MySQL;
      break;
    default:
      return false;
  }
}

// The Default_MySQL class will look like this
class Default_MySQL extends DB_MySQL{

  public function __construct(){
    $this->user 	= "your username";
    $this->pass 	= "your password";
    $this->dbhost = "your database host";
    $this->dbname = "your database name";
  }

}

Now a quick usage example :

include_once("db_mysql.class.php");
include_once("db_statement.class.php");
include_once("mysqexception.class.php");
include_once("connectionfactory.php");

$query    = "INSERT INTO table VALUES (:0, :1, :2, :3);

$oDB      = DB_Connection_Factory("default");
$result   = $oDB->execute($query)
                ->prepare($arg1, $arg2, $arg3, $arg4);

//then a select query

$query    = "SELECT * FROM Table";
$oDB      = DB_Connection_Factory("default");
$result   = $oDB->prepare()->execute($query);
while ($row = $result->fetchAssoc()){
  echo $row['rowName'];
}
If you have any question plese mail me and I will answer them as best as I can. Hope this helps you out and enjoy.


SkyeTX Technologies Business Software Solutions
SkyeTX Technologies Business Software Solutions