Advertisement
Guest User

DBConnection

a guest
Oct 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2.  
  3. class DBConnection extends Mysqli
  4. {
  5.     protected $db;
  6.    
  7.     public function __construct()
  8.     {
  9.         global $config;
  10.         $this->db = parent::__construct($config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbname']);
  11.         if( $this->db->connect_errno )
  12.         {
  13.             die("Error database connection. ".$this->db->connect_error);
  14.             exit;
  15.         }
  16.     }
  17.    
  18.     public function insert($table, $data)
  19.     {
  20.         if( is_array($data) )
  21.         {
  22.             $insert_data = "";
  23.             foreach($data as $column => $value)
  24.             {
  25.                 $insert_data .= $column."='".$this->db->real_escape_string($value)."' ";
  26.             }
  27.             $insert_data = trim($insert_data);
  28.            
  29.             if( $insert_data !== "" )
  30.             {
  31.                 $qry = "INSERT INTO $table SET $insert_data";
  32.                 $a = $this->db->query($qry) or die("Insert query error. ".$this->db->error);
  33.                 return $a;
  34.             }
  35.         }
  36.         else
  37.         {
  38.             return "Insert query error. Inserted data must be in array";
  39.             exit;
  40.         }
  41.     }
  42.    
  43.     public function __destruct()
  44.     {
  45.         $this->db->close();
  46.     }
  47. }
  48.  
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement