Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. class MySQL
  2. {
  3.     protected $mysql;
  4.     function __construct()
  5.     {
  6.                 //Get MySQL config values from config.ini file
  7.         if($config = parse_ini_file("../config.ini"))
  8.         {
  9.             // Obtener los valores del fichero de configuración config.ini
  10.             $ip = $config["ip"];
  11.             $user = $config["usuario"];
  12.             $pass = $config["password"];
  13.             $bd = $config["bd"];
  14.                          
  15.                          //Connection between a database and php
  16.             $this->mysql = new mysqli($ip, $user, $pass, $bd);
  17.         }
  18.     }
  19.  
  20.     function setResultQuery($query, $param)
  21.     {
  22.         $array = NULL;
  23.         if(!$this->mysql->connect_errno)
  24.         {
  25.             $stmt = $this->setStatement($query, $param);
  26.             try
  27.             {
  28.                 if($stmt != NULL)
  29.                 {
  30.                     if($stmt->execute())
  31.                     {
  32.                         //Obtener resultados
  33.                         $stmt->store_result();
  34.                         $variables = array();
  35.                         $data = array();
  36.                         $meta = $stmt->result_metadata();
  37.                         while($field = $meta->fetch_field())
  38.                         {
  39.                             $variables[] = &$data[$field->name];
  40.                         }
  41.                         call_user_func_array(array($stmt, 'bind_result'), $variables);
  42.                         $i=0;
  43.                         while($stmt->fetch())
  44.                         {
  45.                                 $array[$i] = array();
  46.                                 foreach($data as $k=>$v)
  47.                                 $array[$i][$k] = $v;
  48.                                 $i++;
  49.                         }
  50.                         $stmt->close();
  51.                     }
  52.                 }
  53.             }catch(Exception $e){
  54.                 $array = FALSE;
  55.             }
  56.         }
  57.         return $array;
  58.     }
  59.  
  60.     function setStatement($query, $param)
  61.     {
  62.         try
  63.         {
  64.             $stmt = $this->mysql->prepare($query);
  65.             $ref = new ReflectionClass('mysqli_stmt');
  66.             if(count($param) != 0)
  67.             {
  68.                
  69.                 $method = $ref->getMethod('bind_param');
  70.                 $method->invokeArgs($stmt, $param);
  71.             }
  72.         }catch(Exception $e){
  73.             if($stmt != null)
  74.             {
  75.                 $stmt->close();
  76.             }
  77.         }
  78.         return $stmt;
  79.     }
  80.  
  81.     function setNoResultQuery($query, $param)
  82.     {
  83.         $validation = FALSE;
  84.         if(!$this->mysql->connect_errno)
  85.         {
  86.             try
  87.             {
  88.                 $stmt = $this->setStatement($query, $param);
  89.                 if($stmt != null)
  90.                 {
  91.                     if($stmt->execute())
  92.                     {
  93.                         $stmt->close();
  94.                         $validacion = TRUE;
  95.                     }
  96.                 }
  97.             }catch(Exception $e){
  98.                 $validation = FALSE;
  99.             }
  100.         }
  101.         return $validation;
  102.     }
  103.  
  104.     function __destruct()
  105.     {
  106.         $this->mysql->close();
  107.     }
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement