Return to Snippet

Revision: 51150
at September 16, 2011 01:14 by jose_506


Updated Code
<?php
class DatabaseMysql{
    
    //Constants
    const host="localhost";
    const user="root";
    const password="";
    const test=true;
    const database="presupuestos";
    const email_admin="[email protected]";
    
    //Variables privates
    private static $singleInstancia;
    
    
    
    private $connection;
    private $selec_bbdd;
   

    //Constructor
    
    
    /*SINGLETON*/
    private function __construct() 
    {
        //echo 'Estoy construido';
        //$this->database =isset($db)?$db:$this->err();
    }
    

    public static function getInstancia(){
        if(!self::$singleInstancia){
            self::$singleInstancia= new self();
        }
        return self::$singleInstancia;        
    }
    
    function Metodo(){
        var_dump(self::$singleInstancia);
    }
    
    public function __clone()
    {
        trigger_error('No se permite la clonación.', E_USER_ERROR);
    }
    
    
    //Show erros the mysql
    function err(){
        if (self::test){
                echo "<b><font color='red'>ERROR:</b> --> </b>" .
                mysql_errno() . "</b> - <i>" . mysql_error() . "</i></font>";
                exit();
        }else{
                //echo "<b><font color='red'>Ha habido un error." . mysql_error() . "</font></b>";
                if (self::email_admin){
                        //echo ", el administrador ha sido informado por email";
                        mail(self::email_admin,
                        "Error mysql en" . $_SERVER['PHP_SELF'],
                        "Error-> " . mysql_error() .
                        "\n en->" . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'] .
                        "\n a las-> " . date('H:i:s - D-d-m-Y'));
                }
                exit();
        }
    }
        
    public function Connect(){      
        $this->connection = @mysql_connect(self::host, self::user, self::password) or $this->err();
        $this->selec_bbdd = @mysql_select_db(self::database, $this->connection) or $this->err();
    }
    
    // @todo: probar mysql_unbuffered_query() ya que no guarda en buffer y ahorra memoria
    // @todo: ver como hacer la liberacion de consultas grandes o especificar por parametro a la funcion si es consulta grande o no
    //registro seleccionados por el select mysql_num_rows()
    public function Queryarray($query){
        $result=mysql_query($query,$this->connection);

        while($fila = mysql_fetch_array($result,MYSQL_NUM)){
            $arr_num[] = $fila;
        }
        
        return($arr_num);
    }
    
    //Query simple 
    //
     public function Query($query){
        $result=mysql_query($query,$this->connection)or $this->err();
        
        return $result;
    }
    
    // @todo: Definir comentarios
    public function Queryassoc($query){
            $result=mysql_query($query,$this->connection);
            
            while($fila = mysql_fetch_assoc($result)){
                $arr_asoc[] = $fila;
            }
            
            return $arr_asoc;
    }
    
    public function Queryassocwhere($table,$field,$value){
         
        $query="SELECT * FROM $table WHERE $field=$value LIMIT 1";
        
        $result=mysql_query($query,$this->connection);
        
        while($fila = mysql_fetch_assoc($result)){
            $arr_asoc[] = $fila;
        }

        return $arr_asoc;
    }
    
    public function Queryrowwhere($table,$field,$value){
         
        $query="SELECT * FROM $table WHERE $field='$value' LIMIT 1";
        
        $result=mysql_query($query,$this->connection);
        $fila = mysql_fetch_row($result);
        return $fila;
    }
    
    public function QueryObject($select,$table) {
        if(isset($table))
        {
            $fields=isset($select)?$select:'*';
            $query="SELECT $fields FROM $table";
            $result=mysql_query($query,$this->connection);
            return mysql_fetch_object($result);
        }
    }
    
    // mysql_query_retorna true o false si es insert,delete,drrop y update y filas afectadas por la consulta  mysql_affected_rows()
    public function Insertquery($query){
        $result=mysql_query($query)or $this->err();
        
        return $result;
    }
    
    public function Deletequery($field,$value,$table){
        return mysql_query("DELETE FROM `$this->database`.`$table` WHERE `$table`.`$field`=$value",$this->connection);
    }
    
    public function Newidtable($field,$table){
         $query="SELECT MAX($field) AS id_max FROM $table";
         $result=mysql_query($query,$this->connection);
         $valuemax= mysql_fetch_row($result);
         return $valuemax[0];
    }

    public function Close(){
        mysql_close($this->connection) or $this->err();
    }
    
    public function Info(){
        $information="Version mysql: ".  mysql_get_client_info()."<br/>";
        $information.=" Connection and hostname: ".mysql_get_host_info()."<br/>";
        $information.=" Protocol: ".mysql_get_proto_info()."<br/>";
        $information.=" Server version mysql: ".mysql_get_server_info()."<br/>"; 
        $information.=" Server version mysql: ".mysql_get_server_info()."<br/>"; 
        $information.=" ".  mysql_stat($this->connection)."<br/>";
        return $information;
    }
    
    public function ListTable($table,$type=NULL){        
        if(isset($type)){
            return $type=='array'?$this->Queryarray("SELECT * FROM $table"):$this->Queryassoc("SELECT * FROM $table");
        }else{
            return $this->Queryarray("SELECT * FROM $table");
        }
    }
     
    public function ListTableJoin($table1,$table2,$field1,$field2,$select){     
           try{ 
            $fields = isset($select)?$select:'*';
            return $this->Queryassoc("SELECT $fields FROM $table1 AS t1 INNER JOIN $table2 AS t2 ON t1.$field1 = t2.$field2");
            
           }catch(Exception $e){
               echo 'Excepción capturada: ',  $e->getMessage(), "\n";
           }

    }
    
    public function ExistRecords($table,$where=NULL){
        if(isset($where)){
            $result = mysql_query("SELECT * FROM $table WHERE $where", $this->connection) or $this->err();
        }else{
            $result = mysql_query("SELECT * FROM $table", $this->connection) or $this->err();
        }
        return mysql_num_rows($result);
       // return $this->connection;
    }


   
    //@todo: crear funcion de chequeo de coenxion con el servidor o de ping
    

    
    public function get($name){
        return $this->$name;
    }
}
?>

Revision: 51149
at September 16, 2011 00:36 by jose_506


Initial Code
<?php
class DatabaseMysql{
    
    //Constants
    const host="localhost";
    const user="root";
    const password="";
    const test=true;
    const database="presupuestos";
    const email_admin="[email protected]";
    
    //Variables privates
    private static $singleInstancia;
    
    
    
    private $connection;
    private $selec_bbdd;
   

    //Constructor
    
    
    /*SINGLETON*/
    private function __construct() 
    {
        //echo 'Estoy construido';
        //$this->database =isset($db)?$db:$this->err();
    }
    

    public static function getInstancia(){
        if(!self::$singleInstancia){
            self::$singleInstancia= new self();
        }
        return self::$singleInstancia;        
    }
    
    function Metodo(){
        var_dump(self::$singleInstancia);
    }
    
    public function __clone()
    {
        trigger_error('No se permite la clonación.', E_USER_ERROR);
    }
    
    
    //Show erros the mysql
    function err(){
        if (self::test){
                echo "<b><font color='red'>ERROR:</b> --> </b>" .
                mysql_errno() . "</b> - <i>" . mysql_error() . "</i></font>";
                exit();
        }else{
                //echo "<b><font color='red'>Ha habido un error." . mysql_error() . "</font></b>";
                if (self::email_admin){
                        //echo ", el administrador ha sido informado por email";
                        mail(self::email_admin,
                        "Error mysql en" . $_SERVER['PHP_SELF'],
                        "Error-> " . mysql_error() .
                        "\n en->" . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'] .
                        "\n a las-> " . date('H:i:s - D-d-m-Y'));
                }
                exit();
        }
    }
        
    public function Connect(){      
        $this->connection = @mysql_connect(self::host, self::user, self::password) or $this->err();
        $this->selec_bbdd = @mysql_select_db(self::database, $this->connection) or $this->err();
    }
    
    // @todo: probar mysql_unbuffered_query() ya que no guarda en buffer y ahorra memoria
    // @todo: ver como hacer la liberacion de consultas grandes o especificar por parametro a la funcion si es consulta grande o no
    //registro seleccionados por el select mysql_num_rows()
    public function Queryarray($query){
        $result=mysql_query($query,$this->connection);

        while($fila = mysql_fetch_array($result,MYSQL_NUM)){
            $arr_num[] = $fila;
        }
        
        return($arr_num);
    }
    
    //Query simple 
    //
     public function Query($query){
        $result=mysql_query($query,$this->connection)or $this->err();
        
        return $result;
    }
    
    // @todo: Definir comentarios
    public function Queryassoc($query){
            $result=mysql_query($query,$this->connection);
            
            while($fila = mysql_fetch_assoc($result)){
                $arr_asoc[] = $fila;
            }
            
            return $arr_asoc;
    }
    
    public function Queryassocwhere($table,$field,$value){
         
        $query="SELECT * FROM $table WHERE $field=$value LIMIT 1";
        
        $result=mysql_query($query,$this->connection);
        
        while($fila = mysql_fetch_assoc($result)){
            $arr_asoc[] = $fila;
        }

        return $arr_asoc;
    }
    
    public function Queryrowwhere($table,$field,$value){
         
        $query="SELECT * FROM $table WHERE $field='$value' LIMIT 1";
        
        $result=mysql_query($query,$this->connection);
        $fila = mysql_fetch_row($result);
        return $fila;
    }
    
    public function QueryObject($select,$table) {
        if(isset($table))
        {
            $fields=isset($select)?$select:'*';
            $query="SELECT $fields FROM $table";
            $result=mysql_query($query,$this->connection);
            return mysql_fetch_object($result);
        }
    }
    
    // mysql_query_retorna true o false si es insert,delete,drrop y update y filas afectadas por la consulta  mysql_affected_rows()
    public function Insertquery($query){
        $result=mysql_query($query)or $this->err();
        
        return $result;
    }
    
    public function Deletequery($field,$value,$table){
        return mysql_query("DELETE FROM `$this->database`.`$table` WHERE `$table`.`$field`=$value",$this->connection);
    }
    
    public function Newidtable($field,$table){
         $query="SELECT MAX($field) AS id_max FROM $table";
         $result=mysql_query($query,$this->connection);
         $valuemax= mysql_fetch_row($result);
         return $valuemax[0];
    }

    public function Close(){
        mysql_close($this->connection) or $this->err();
    }
    
    public function Info(){
        $information="Version mysql: ".  mysql_get_client_info()."<br/>";
        $information.=" Connection and hostname: ".mysql_get_host_info()."<br/>";
        $information.=" Protocol: ".mysql_get_proto_info()."<br/>";
        $information.=" Server version mysql: ".mysql_get_server_info()."<br/>"; 
        $information.=" Server version mysql: ".mysql_get_server_info()."<br/>"; 
        $information.=" ".  mysql_stat($this->connection)."<br/>";
        return $information;
    }
    
    public function ListTable($table,$type=NULL){        
        if(isset($type)){
            return $type=='array'?$this->Queryarray("SELECT * FROM $table"):$this->Queryassoc("SELECT * FROM $table");
        }else{
            return $this->Queryarray("SELECT * FROM $table");
        }
    }
     
    public function ListTableJoin($table1,$table2,$field1,$field2,$select){     
           try{ 
            $fields = isset($select)?$select:'*';
            return $this->Queryassoc("SELECT $fields FROM $table1 AS t1 INNER JOIN $table2 AS t2 ON t1.$field1 = t2.$field2");
            
           }catch(Exception $e){
               echo 'Excepción capturada: ',  $e->getMessage(), "\n";
           }

    }
    
    public function ExistRecords($table,$where=NULL){
        if(isset($where)){
            $result = mysql_query("SELECT * FROM $table WHERE $where", $this->connection) or $this->err();
        }else{
            $result = mysql_query("SELECT * FROM $table", $this->connection) or $this->err();
        }
        return mysql_num_rows($result);
       // return $this->connection;
    }


   
    //@todo: crear funcion de chequeo de coenxion con el servidor o de ping
    

    
    public function get($name){
        return $this->$name;
    }
}
?>

Initial URL


Initial Description


Initial Title
Class database mysql

Initial Tags
mysql, database, class, php

Initial Language
PHP