Guest User

Untitled

a guest
Nov 19th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. <?php
  2.  
  3. final class MySQL
  4. {
  5.     private $link;
  6.     private $numMySQL;
  7.  
  8.     public function __construct()
  9.     {
  10.  
  11.  
  12.         $arr_host = explode(',', $host);
  13.         $bd_host = (count($arr_host) > 1) ? $arr_host[mt_rand(0, count($arr_host) - 1)] : $arr_host[0];
  14.  
  15.         $this->numMySQL = 0;
  16.         $this->link = mysqli_connect($host, $user, $pass, $db_name);
  17.         if (mysqli_connect_errno($this->link)) {
  18.             trigger_error("Нет соединения с MySQL");
  19.         }
  20.  
  21.         if (!mysqli_ping($this->link)) {
  22.             $bd_host = (count($arr_host) > 1) ? $arr_host[mt_rand(0, count($arr_host) - 1)] : $arr_host[0];
  23.             $this->link = mysqli_connect($host, $user, $pass, $db_name);
  24.         }
  25.  
  26.         mysqli_set_charset($this->link, 'utf8');
  27.  
  28.     }
  29.  
  30.     public function query($sql)
  31.     {
  32.         $this->numMySQL++;
  33.         $resource = mysqli_query($this->link, $sql);
  34.         if ($resource) {
  35.             if (is_object($resource)) {
  36.  
  37.                 $i = 0;
  38.  
  39.                 $data = array();
  40.  
  41.                 while ($result = mysqli_fetch_assoc($resource)) {
  42.                     $data[$i] = $result;
  43.                     $i++;
  44.                 }
  45.  
  46.                 mysqli_free_result($resource);
  47.  
  48.                 $query = new stdClass();
  49.                 $query->row = isset($data[0]) ? $data[0] : array();
  50.                 $query->rows = $data;
  51.                 $query->num_rows = $i;
  52.  
  53.                 unset($data);
  54.                 return $query;
  55.  
  56.             } else {
  57.  
  58.                 $query = new stdClass();
  59.                 $query->row = array();
  60.                 $query->rows = array();
  61.                 $query->num_rows = 0;
  62.  
  63.                 return $query;
  64.  
  65.             }
  66.         } else {
  67.  
  68.             $query = new stdClass();
  69.             $query->row = array();
  70.             $query->rows = array();
  71.             $query->num_rows = 0;
  72.  
  73.             return $query;
  74.  
  75.         }
  76.     }
  77.  
  78.     public function __destruct()
  79.     {
  80.         mysqli_close($this->link);
  81.     }
  82. }
  83.  
  84. ?>
Add Comment
Please, Sign In to add comment