All pastes #956217 Raw Edit

Something

public php v1 · immutable
#956217 ·published 2008-03-25 02:45 UTC
rendered paste body
<? require_once('dbexcept.class.php'); abstract class DBa {   protected $host, $username, $password;   protected $error = null; 	  public $handle, $persistant, $flags;  public $query;   public $result = null;   private $connected = false;     abstract public function connect();   abstract public function close();   abstract public function error();  abstract public function query($query);   abstract public function escape($string);     private function __construct($connect=false, $host=null, $user=null, $pass=null, $database=null, $persist=false, $new=false) {     list($this->host, $this->username, $this->password, $this->persistant, $this->database) 	  = array($host, $user, $pass, $database, $database, $persist);    if($connect)      $this->connect($host, $user, $pass, $database, $persist, $new);   }   public function connected() { return $this->connected; }   public function exec($query) { /* This will NOT return OR assign a result object */     if($this->query($query) !== false)	    return $this->affected(); 	throw(new DBException('Query execution failed.'));   } }  ?> <? /******************************************************************  *   -db.class.php:  Database control, An easier way to define/change behavior later   *       on.. or port the entire mess to another type of database altogether, etc..   *  *   In atleast this instance, employ exceptions as opposed to indiscriminate default   *   error messages.. to avoid releasing any sensitive information they might contain,   *   and to provide to infinite control over the actions taken in such an event.   *   further force desired options, as to not permit assumptions to be made by   *     the functions called or php itself  *   *  (C) 2008 and beyond, Vanguard Technical & Media Services   *   - Author:  Bryan Steimel 3/22/08  ******************************************************************/ class db_result {  public $resource;    private $data = array();    private function __construct($result) {     $this->resource = $result;  }     public function next($list=false) { $this->data = @mysql_fetch_array($this->resource, $list ? MYSQL_NUM : MYSQL_ASSOC); }     public function rows() { return @mysql_num_rows($this->resource); }   public function free() {  @mysql_free_result($this->resource);  }     private function __set($indice, $value) { return $this->data[$indice] = $value; }  private function __get($indice) { return isset($this->data[$indice]) ? $this->data[$incide] : null;  }  private function __destruct() { $this->free(); } } /* should probably be generic/abstract definition/interface, to aid in forcing further implementation, *  but for these specific purposes I dont see the point in putting forth   * the additional trouble - besides, theres far too many issues with PHP's implementation   * of interface */  class DB extends DBa {   public function connect($host=null, $user=null, $pass=null, $database=null, $persist=false, $new=false) {     $this->_construct(false, $host, $user, $pass, $database, $persist, $new);     if($this->connected = (	  ($this->handle = $this->persistant 	    ? @mysql_pconnect($this->host, $this->username, $this->password, $new, $this->flags) 	      : @mysql_connect($this->host, $this->username, $this->password, $new, $this->flags)        ) !== false) 	  ? true : false) return $this->handle;    throw(new DBException('Failed to establish a connection to the requested host on port ' . $port . '.');   }     public function close() { return @mysql_close($this->handle); }    public function select_db($db) {     $this->database = $db;   	return $r = @mysql_select_db($this->database, $this->handle) 	  ? $r         : throw(new DBException('Failed to select database \'' . $this->database . '\'.'));   }     public function query($query = null) {     if(!is_null($query)) 	  $this->query = $query; /* re-execute LAST query if null */     if(($result = @mysql_query($this->query, $this->handle)) !== false)	  return is_resource($result) 	    ? ($this->result = new db_result($result))		  : $result; 	throw new DBException('Query failed.');   }     public function exec($query) { /* This will NOT return OR assign a result object */ 	if(is_object($result = parent::exec($query)))	  throw(new DBException('exec(): INSERT/DELETE/UPDATE only'));	else      return $result;	  }     public function insert($table, $values = array(), $data = array()) {     if(!empty($values) && empty($data)) 	    }     public function affected() { return mysql_affected_rows($this->resource); }  public function escape($string) { return mysql_real_escape_string($string); }     public function error() { return $this->error = mysql_error(); }     private function __destruct() {     } }  ?>