<?php
class mySql extends ezSQL_mysqli{

    var $log = NULL;
    
    public function row($table, $fields='*', $where=null, $order=null, $limit=null){
        $q = "SELECT $fields FROM $table" . $this->whereis($where) . $this->orderis($order) . $this->limitis($limit);
        $this->log.= $q."\r\n";
        return $this->get_row($q)?$this->get_row($q):null ;
    }
    
   
    public function getVar($table, $fields='*', $where=null){
        $q = "SELECT $fields FROM $table" . $this->whereis($where);
        $this->log.= $q."\r\n";
        return $this->get_row($q) ? $this->get_var($q) : "";
        
    }
    
    
    function queryresults($q){
        $this->log.= $q."\r\n";
        return $this->get_results($q);
    }
    
    function results($table, $fields='*', $where=null, $order=null, $limit=null){
        $q = "SELECT $fields FROM $table" . $this->whereis($where) . $this->orderis($order) . $this->limitis($limit);
        $this->log.= $q."\r\n";
        return $this->get_results($q);
    }
    
    function UltimoId($table=NULL){
        if($table) return $this->getVar($table, 'MAX(id)');
        return $this->insert_id;
    }
    

    public function delete($table,$where=NULL){
        if($table && $where){
            $q = "DELETE FROM $table" . $this->whereis($where);
            $this->log.= $q."\r\n";
            return $this->query($q);
        }
    }
    
    public function update($table,$datos,$where){
        if($table && is_array($datos) && $where){
            $values = array();
            foreach($datos as $k=>$v){
                $values[] = "$k=$v";
            }
            $values = implode(', ',$values); //trim(trim($values),',');
            $q = "UPDATE $table SET $values" . $this->whereis($where);
            $this->query($q);
            $this->log.= 'Update : ' . $q . "\r\n";
			$this->rows_affected = @$this->dbh->affected_rows;
			$return_val = $this->rows_affected;
            return $return_val;
        }
    }
    
    
    function insert($table,$datos){
        if($table && is_array($datos)){
            $fields = array();
            $values = array();
            foreach($datos as $k=>$v){
                $fields[] = $k;
                $values[] = $v;
            }
            $fields = implode(', ',$fields) ;
            $values = implode(', ',$values) ;
            $q = "INSERT INTO $table ($fields) VALUES ($values)";

            $this->query($q);
            $this->log.= 'Insert : '.$q."\r\n";
            return $this->insert_id;
        }
    }
   
   
    
    function whereis($where=null){
        if (!empty($where)){ return " WHERE $where "; }else{ return ""; }
    }
    
    function orderis($order=null){
        if(!empty($order)){ return " ORDER BY $order "; }else{ return ""; }
    }
    
    function limitis($limit=null){
        if(!empty($limit)){ return " LIMIT $limit ";  }else{ return ""; }
    }
    
    function string($string,$html=null){
        $string = trim($string);
        if(!isset($html)) $string = strip_tags($string);
        /*if(@get_magic_quotes_gpc()) { return "'".$string."'"; }
                               else { return "'".addslashes($string)."'"; }*/
        //return $string;
        return "'".$string."'";
    }
    
}