Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/09/2011, 00:27
Avatar de BaByRoDrI
BaByRoDrI
 
Fecha de Ingreso: julio-2009
Ubicación: Mexico
Mensajes: 60
Antigüedad: 14 años, 9 meses
Puntos: 1
Pregunta Clase para conectar con base de datos

Googleando un poco y llendo de blog en blog, he encontrado una clase en PHP que al parecer permite conectar con una BD, lo bonito de la cosa es que creo que si se le añaden cosas se puede conectar con varios tipos de BD, aquí les dejo el source:

Código PHP:
Ver original
  1. <?php
  2. class DataAccess
  3. {
  4.     private $tipo = 'mysql';
  5.     private $host = 'localhost';
  6.     private $user = 'root';
  7.     private $pass = '';
  8.     private $base = 'ajax';
  9.    
  10.     private $conexion;
  11.     private $errorNumbers;
  12.    
  13.     private static $instance=null;
  14.    
  15.     private function __construct()
  16.     {
  17.         switch($this->tipo)
  18.         {
  19.             case 'mysql':
  20.             $this->errorList=array('duplicate_entry'=>1062);
  21.             $this->conexion=@mysql_connect($this->host, $this->user, $this->pass);
  22.  
  23.             if(!$this->conexion) return false;
  24.             else
  25.             {
  26.                 return @mysql_select_db($this->base);
  27.             }
  28.             break;
  29.         }
  30.     }
  31.    
  32.     public static function GetInstance()
  33.     {
  34.         if(self::$instance==null) self::$instance=new self();
  35.         return self::$instance;
  36.     }
  37.    
  38.     public function Disconnect()
  39.     {
  40.         switch($this->tipo)
  41.         {
  42.             case 'mysql':
  43.             return ($this->conexion=@mysql_close($this->connection));
  44.             break;
  45.         }
  46.     }
  47.    
  48.     public function LastErrorNumber()
  49.     {
  50.         switch($this->tipo)
  51.         {
  52.             case 'mysql':
  53.             return @mysql_errno($this->conexion);
  54.             break;
  55.         }
  56.     }
  57.    
  58.     public function LastErrorMessage()
  59.     {
  60.         switch($this->tipo)
  61.         {
  62.             case 'mysql':
  63.             return @mysql_error($this->conexion);
  64.             break;
  65.         }
  66.     }
  67.    
  68.     public function ErrorList($errorID)
  69.     {
  70.         return $this->errorList[$errorID];
  71.     }
  72.    
  73.     public function Query($query)
  74.     {
  75.         switch($this->tipo)
  76.         {
  77.             case 'mysql':
  78.             return @mysql_query($query);
  79.             break;
  80.         }
  81.     }
  82.    
  83.     public function GetRegistry($result)
  84.     {
  85.         switch($this->tipo)
  86.         {
  87.             case 'mysql':
  88.             return @mysql_fetch_array($result);
  89.             break;
  90.         }
  91.     }
  92.    
  93.     public function Escape($string)
  94.     {
  95.         switch($this->tipo)
  96.         {
  97.             case 'mysql':
  98.             return @mysql_real_escape_string($string);
  99.             break;
  100.         }
  101.     }
  102.    
  103.     public function NumRows($result)
  104.     {
  105.         switch($this->tipo)
  106.         {
  107.             case 'mysql':
  108.             return @mysql_num_rows($result);
  109.             break;
  110.         }
  111.     }
  112. }
  113. ?>

Por desgracia no he sabido utilizarla, si alguien puede ayudarme a utilizarla, porque se ve bastante bien esta clase!