<?php
require_once './pdoparameters.php';
/*
*@Autor DNDSIT 
*/
class PDOCONTROLLER{
	private $cadenaconex;
	private $conex;
	
	public function __construct(){
		$conex = $this->connectDB();
		if(!empty($conex)) {
			$this->conex = $conex;			
		}
		
	}

	function connectDB() {
		try{
			$this->cadenaconex="mysql:host=".DB_SERVER.";port=".DB_PORT.";dbname=".DB_DBNAME.";charset=".DB_CHARSET;
			$options=[
				PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
				PDO::ATTR_EMULATE_PREPARES => false,
			];
			$pdo = new PDO($this->cadenaconex,DB_USER,DB_PASS,$options);
			return $pdo;

		}catch(Exception $ex){
			echo "Error conexions".$ex->getMessage();
			exit;
		}

	}
	
	public function executeconsulta($sql="",$valores=array()){
		//echo $sql;
		//echo $conex;
		if($sql != "" && strlen($sql)>0){
			echo json_encode($valores);
			try{
				$consulta=$this->conex->prepare($sql);
				//$consulta = $this->$conex->query($sql);
				$consulta->execute($valores);
				if(intval($consulta->errorCode())===0){
					
					return $consulta->fetchAll(PDO::FETCH_ASSOC);
				}else{
					return intval($consulta->errorCode());
				}
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}


}

//$dao=new PDOCONTROLLER();

?>