Advertisement
Guest User

class_mysqli.php

a guest
Apr 20th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?PHP
  2. /**
  3.  *
  4.  * NOTE: Designed for use with PHP version 5.5 and up
  5.  * @author T0niiiiii <t0niiiiii@>
  6.  * @copyright 2016
  7.  * @version: 1.0
  8.  *
  9.  */
  10.  
  11. /**
  12.  * Class MySQLI file.
  13.  */
  14.  
  15. Class MySql {
  16.        
  17.     Private $connected;
  18.     Private $connection;
  19.        
  20.     Public Function __construct($host, $user, $password, $db)
  21.     {
  22.         $this->host = $host;
  23.         $this->db = $db;
  24.         $this->user = $user;
  25.         $this->password = $password;
  26.            
  27.         $this->connect();
  28.     }
  29.        
  30.     Private Function connect()
  31.     {
  32.         if(!$this->connected){
  33.             if($this->connection = @new mysqli($this->host, $this->user, $this->password)){
  34.                 if($database = $this->connection->select_db($this->db)){
  35.                     $this->connection->set_charset("UTF-8");
  36.                     $this->connected = true;
  37.                 } else {
  38.                     return print $this->connection->connect_error;
  39.                 }
  40.             } else {
  41.                 return print $this->connection->connect_error;
  42.             }
  43.         }
  44.     }
  45.        
  46.     Private Function disconnect()
  47.     {
  48.         if($this->connected){
  49.             $this->connection->kill($this->connection->thread_id);
  50.             if($this->connection->close()){
  51.                 $this->connected = null;
  52.             } else {
  53.                 return print $this->connection->connect_error;
  54.             }
  55.         }
  56.     }
  57.        
  58.     Public Function query($sql)
  59.     {
  60.         return $this->connection->query($sql);
  61.     }
  62.        
  63.     Public Function result($sql)
  64.     {
  65.         if($this->connected){
  66.             $query = $this->query($sql);
  67.             if($query){
  68.                 $result = array();
  69.                 $i = 0;
  70.                 while($row = $query->fetch_object()){
  71.                     $result[$i] = $row;
  72.                     $i++;
  73.                 }
  74.                 return $result;
  75.             } else {
  76.                 return print $this->connection->error;
  77.             }
  78.         }
  79.     }
  80.  
  81.     Public Function result_($sql)
  82.     {
  83.         if($this->connected){
  84.             if($query = $this->query($sql)){
  85.                 $result = array();
  86.                 while($row = $query->fetch_object()){
  87.                     $result[0] = $row;
  88.                 }
  89.                 return $result;
  90.             } else {
  91.                 return print $this->connection->error;
  92.             }
  93.         }
  94.     }
  95.  
  96.     Public Function secure($data)
  97.     {
  98.         if ($this->connected){
  99.             return $this->connection->real_escape_string(htmlspecialchars(strip_tags(trim($data))));
  100.         }
  101.         return print $this->connection->error;
  102.     }
  103.        
  104.     Public Function __destruct()
  105.     {
  106.         $this->disconnect();
  107.     }
  108.  
  109. }
  110.    
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement