<?php
$MYSQL_ERRNO ='';
$MYSQL_ERROR ='';

class DB{
  private $dbhost = 'localhost';
  private $dbusername = 'wcadexpr_mobproye_hadmon'; 
  private $dbusepassword = 'F4etCc.Vp}36';  
  private $default_dbdame = 'wcadexpr_mobproye_hadmon';
  private $conectado;
  private $registros=array();
  private $respuesta ="";

  public function __construct()
  {
    
    /*$dbhost = 'localhost';
    $dbusername = 'root'; 
    $dbusepassword = ''; 
    $default_dbdame = 'crm_dicss';
    $this->dbhost=$dbhost;
    $this->dbusername=$dbusername;
    $this->dbusepassword=$dbusepassword;
    $this->default_dbdame=$default_dbdame;*/
  }

public static function getInstance()//para traer la misma conexion y no hacer mas peticiones de conexiones a la db
   {
      if (  !self::$instancia instanceof self)
      {
         self::$instancia = new self;
      }
      return self::$instancia;
   }


function conectando(){
   $host=$this->dbhost;
   $usuario=$this->dbusername;
   $contrasena=$this->dbusepassword;
   $DB=$this->default_dbdame;

   $link_id = mysqli_connect($host, $usuario, $contrasena, $DB);
  if (!$link_id){
    $MYSQL_ERRNO = 0;
    $MYSQL_ERROR = "La coneccion ha fallado. " ;
        printf("NO HAY CONEXION CON LA BASE dicss\n");
        exit();
  }
  if(!mysqli_select_db($link_id,$DB) ) {
    $MYSQL_ERRNO = mysql_errno();
    $MYSQL_ERROR = mysql_error();
        printf("NO HAY CONEXION CON LA BASE dicss\n");
    exit();
  }
  $this->instancia=$link_id;
 return $link_id;
}

function consultaregistro($campo, $id, $tabla){
$host=$this->dbhost;
$usuario=$this->dbusername;
$contrasena=$this->dbusepassword;
$DB=$this->default_dbdame;
if($id!=""){
$kuery="SELECT $campo FROM `$tabla` where id_menu = $id";
  }else{
$kuery="SELECT $campo FROM `$tabla` ";
}
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
  while ($reg= @mysqli_fetch_assoc($consulta)){
 $this->registros[]=$reg;
}
return $this->registros;
}


function update($tabla, $campo, $valorlabelnuevo, $id){
$kuery = "UPDATE  `$tabla` SET  `$campo` =  '$valorlabelnuevo' WHERE  `id_$tabla` =$id;";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
$consulta=$kuery;
}
return $consulta;
}

function updateg($sentencia){
$kuery = $sentencia;
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
$consulta=$kuery;
}
return $consulta;
}


function updateGen($tabla, $campo, $valorlabelnuevo,$id ,$idvalor){
$con=$this->conectando();
$kuery = "UPDATE  `$tabla` SET  `$campo` =  '$valorlabelnuevo' WHERE `$id` ='$idvalor'";
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
  return $kuery;
}else{
  return $consulta;
}
}
    
    
function insertGen($valores, $tabla){
$con=$this->conectando();
$kuery="INSERT INTO `$tabla`  VALUES ($valores);";
$consulta=mysqli_query($con, $kuery);
if(!$consulta)
  return $kuery;
else
  return 1;
}

function deleteGen($tabla, $campo, $valor){
$kuery = "Delete from $tabla where $campo =$valor;";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
  $consulta=$kuery;
}
return $consulta;
}

function delete($tabla, $id){
$kuery = "Delete from $tabla where id_$tabla =$id;";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
  $consulta=$kuery;
}
return $consulta;
}
    
function deleteg($tabla, $id,$campo,$tipo){
$kuery = "Delete from $tabla where $campo =$id and tipo=$tipo;";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
  $consulta=$kuery;
}
return $consulta;
}

function consultaregistrologin($campo, $usr, $pass){
$host=$this->dbhost;
$usuario=$this->dbusername;
$contrasena=$this->dbusepassword;
$DB=$this->default_dbdame;
$kuery="SELECT * FROM usuarios where BINARY usuario = '$usr' and BINARY contrasena = '$pass';";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
while ($reg= @mysqli_fetch_assoc($consulta)){
 $this->registros[]=$reg;
}
return $this->registros;
}

function consultaregistros($kuery){
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);
while ($reg= @mysqli_fetch_assoc($consulta)){
 $this->registros[]=$reg;
}
if(!$consulta){
  return $kuery;
}
return $this->registros;
}

function consultaregistrossalida($kuery){
$region="-1";
$con=$this->conectando();
$consulta=mysqli_query($con, $kuery);

$result = mysqli_affected_rows($con);
while ($reg= @mysqli_fetch_assoc($consulta)){
 $this->registros[]=$reg;
 $region="1";
}
if($result>0){
  $result="";
}
return $result;
}

function seter($valores, $tabla){//para inserts
$suma="";
foreach($valores as $key=>$value){
$suma.=$value.", ";
}
$valores = substr($suma, 0, -2);
$con=$this->conectando();
$kuery="INSERT INTO `$tabla`  VALUES ($valores);";
$consulta=mysqli_query($con, $kuery);
if(!$consulta){
  return $kuery;
}else{
  return $consulta;
}
}

function seterbyid($valores, $tabla){//para inserts
$suma="";
foreach($valores as $key=>$value){
$suma.=$value.", ";
}
$valores = substr($suma, 0, -2);
$con=$this->conectando();
$kuery="INSERT INTO `$tabla`  VALUES ($valores);";
$consulta=mysqli_query($con, $kuery);

if(!$consulta){
  return $kuery;
}else{
  return  mysqli_insert_id($con);
}
}

function updates($campos, $valoresnuevoss, $tabla, $id){//para updates
$con=conectando();
$consulta=mysql_query($kuery,$con);
  while ($row= @mysqli_query($con, $kuery)){
  $this->registros[].=$row['nombre'];
  }
  return $this->registros;
}

}// fin class
?>
