Beruflich Dokumente
Kultur Dokumente
Pagina iniDB.php <?php //parametros de inicio de la base de datos DBALL::$host="localhost"; DBALL::$usr="root"; DBALL::$pass="mysql"; DBALL::$dbname="logico"; DBALL::$motor="mysql"; DBALL::$showErrors=true; ?> //la ruta del servidor donde esta la BD //el usuario para conectarse //el password para conectarse //nombre de la base de datos //el motor puede ser mysql,postgres o isis // indica si se deben mostrar los errores de la BD
Pagina DBALL.php <?php include("iniDB.php"); //archivo que inicia las variables estticas, SIRVE PARA TRABAJAR CON UN SOLO MOTOR DE BASE DATOS class DBALL{ public static $host; public static $usr; public static $pass; public static $dbname; public static $motor; private $conex; private $result; public static $showErrors; public function dbConnect(){ self::$motor=strtoupper(self::$motor); $v=false; if(self::$motor=="MYSQL"){ $this->conex=mysql_connect(self::$host,self::$usr,self::$pass); $var=mysql_select_db(self::$dbname,$this->conex); if($var){ $v=true; } } else if(self::$motor=="PSQL"){ $this->conex=@pg_connect("host=".self::$host." dbname=".self::$dbname." user=".self::$usr." password=".self::$pass); if($this->conex){ $v=true; } } else if(self::$motor=="ISIS"){ $this->conex=isis_open(self::$dbname,"-v error -format aligned -encoding ISO8859_1"); if($this->conex){ $v=true; } }
//devuelve true si se conecto y false si hay error return $v; } public function dbSelect($consulta){ $res=array(); if(self::$motor=="MYSQL"){ $this->result = mysql_query($consulta); if(self::$showErrors){ if(mysql_errno($this->conex)!=0) echo "Error ".mysql_errno($this->conex)." : ".mysql_error($this->conex); } $fila= mysql_num_rows($this->result); $i=0; while ($row = mysql_fetch_object($this->result)){ $res[$i]=$row; $i++; } mysql_free_result($this->result); }else if(self::$motor=="PSQL"){ $this->result = @pg_query($this->conex,$consulta); $fila=@pg_numrows($this->result); for($i=0;$i<$fila;$i++){ $res[$i]=@pg_fetch_object($this->result,$i); } @pg_free_result($this->result); }else if (self::$motor=="ISIS"){ $this->result = isis_query($consulta,$this->conex); $i=0; if ($this->result!=null && $this->result!='') { while($record = isis_fetch_flat_array($this->result)){ $res[$i]=$record; $i++; } } } return $res; } public function dbABM($consulta){ $r=false; if(self::$motor=="MYSQL"){ $this->result = mysql_query($consulta); if(self::$showErrors){ if(mysql_errno($this->conex)!=0) echo "Error ".mysql_errno($this->conex)." : ".mysql_error($this->conex); } //mysql_free_result($this->result); }else if(self::$motor=="PSQL"){ $this->result = @pg_query($this->conex,$consulta); pg_free_result($this->result); }
if ($this->result) { $r=true; } //Devuelve false en caso de error return $r; } public function dbBeginTransaction(){ if(self::$motor=="MYSQL"){ $this->result = mysql_query("BEGIN"); }else{ $this->result = pg_query($this->conex,"BEGIN"); } if ($this->result) { return true; //echo "Transaccion Iniciada"; } else return false; } public function dbCancelTransaction(){ if(self::$motor=="MYSQL"){ $this->result = mysql_query("ROLLBACK"); }else{ $this->result = pg_query($this->conex,"ROLLBACK"); } if ($this->result) { return true;//$r=true; //echo "Transaccion Cancelada"; } else return false; }
public function dbEndTransaction(){ if(self::$motor=="MYSQL"){ $this->result = mysql_query("COMMIT"); }else{ $this->result = pg_query($this->conex,"COMMIT"); } if ($this->result) { return true; //echo "Transaccion Terminada"; } else return false; } public function dbClose(){ if(self::$motor=="MYSQL"){ mysql_close();
}else if(self::$motor=="PSQL"){ pg_close(); }else if (self::$motor=="ISIS"){ @isis_close($this->conex);//$this->dbname,"-v error -format aligned -encoding ISO8859_1" } }
} ?>
Para terminar deben guardar los dos archivos "DBALL.php" y "iniDB.php" en el mismo directorio.
Bueno eso es todo, tb. tiene metodo para manejar transacciones pero eso es mas facil .