<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Conexion{
    public function __construct()
    {
      $this->Conexion = null; 
    }
	Public function get_conexion($db2 = null,$User1= null,$Pass1=null, $host1 =null){
    $User= $User1 == null ? DbUser:$User1;
    $Pass= $Pass1 ==null? DbPass:$Pass1;
    $host= $host1==null? DbHost:$host1;
    $db= $db2==null ?DbName: $db2;
    $this->Conexion = new PDO("mysql:host=$host;dbname=$db;charset=utf8",$User, $Pass );
    return $this->Conexion;
    }
    Public function get_Cerrar(){
    $this->Conexion= null;
    }
}
class Consultas{

    public function AgregarPDO($Campos,$sql,$db = null,$User1= null,$Pass1=null, $host1 =null){
     $Modelo = new Conexion();
     $Conexion = $Modelo->get_conexion($db,$User1,$Pass1, $host1);
        $statement= $Conexion->prepare( $sql);
        while (current($Campos)!==false) {
            
            $statement->bindParam(':'.key($Campos),$Campos[key($Campos)]);

       next($Campos);
        }   
        if(!$statement){
          
           // $statement->closeCursor();
        $statement = null;
        $Modelo->get_Cerrar();
        
            return "Error";

        }else{
           
           $resl= $statement->execute();
          
            $statement->closeCursor();
            $statement = null;
            $Modelo->get_Cerrar();
            if (strpos($sql, 'INSERT') !== false OR strpos($sql, 'insert') !== false ) {
                $id = $Conexion->lastInsertId();
                return  $id;
            }
            return  $resl;

        }

    }


    public function BuscarPDO($sql,$Campos = null,$db = null,$User1= null,$Pass1=null, $host1 =null){
        $rows = null;
        $modelo = new Conexion();
        $conexion = $modelo->get_conexion($db,$User1,$Pass1, $host1);
        $conexion->beginTransaction();
        $statement = $conexion->prepare($sql);
        if ($Campos!=null){
          while (current($Campos)!==false) {
            $statement->bindParam(':'.key($Campos),$Campos[key($Campos)]);
       next($Campos);
        }
        }
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        $statement->execute();
        while ($Consulta = $statement->fetch()){
            $rows[] = $Consulta;
        }
        $statement->closeCursor();
        $statement = null;
        $modelo->get_Cerrar();
        return $rows;
    }   
}

?>