Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. class Database extends mysqli
  4. {
  5.  
  6.     private $host;
  7.     private $user;
  8.     private $pass;
  9.     private $dbName;
  10.     private $connect;
  11.     private $result;
  12.  
  13.     public function __construct($host, $user, $pass, $dbname)
  14.     {
  15.         $this->host = $host;
  16.         $this->user = $user;
  17.         $this->pass = $pass;
  18.         $this->dbName = $dbname;
  19.         $this->connect();
  20.     }
  21.  
  22.     public function connect()
  23.     {
  24.         $this->connect = mysqli_connect($this->host, $this->user, $this->pass, $this->dbName) or
  25.         die("Connection to Database failed.");
  26.     }
  27.  
  28.     public function query($sql)
  29.     {
  30.         $result = $this->connect->query($sql);
  31.         return $result;
  32.     }
  33.  
  34.     public function createTable()
  35.     {
  36.         $sql = "CREATE TABLE products (id INT NOT NULL AUTO_INCREMENT, name TEXT,
  37. price NUMERIC(15,2), description TEXT, PRIMARY KEY (id))";
  38.         return $this->connect->query($sql);
  39.     }
  40.  
  41.     public function insert($tableName, $data)
  42.     {
  43.         $sql = "INSERT INTO {$tableName}";
  44.  
  45.         $keys = "";
  46.         $values = "";
  47.  
  48.         foreach ($data as $key => $value)
  49.         {
  50.             $keys .= $key . ",";
  51.             $values .= "'".$value . "',";
  52.         }
  53.  
  54.         $sql .= "(" . substr($keys, 0, -1). ") VALUES(".substr($values,0,-1).")";
  55.         return $this->connect->query($sql);
  56.     }
  57.  
  58.     public function update($tableName, $data, $id)
  59.     {
  60.         $sql = "UPDATE {$tableName} SET";
  61.  
  62.         $values = "";
  63.  
  64.         foreach ($data as $key => $value)
  65.         {
  66.             $values .= $key. "=" . "" . $value . ",";
  67.         }
  68.  
  69.         $sql .= substr($values, 0, -2). " WHERE id = {$id}";
  70.  
  71.         return $this->connect->query($sql);
  72.     }
  73.  
  74.     public function resetTable($tableName)
  75.     {
  76.         $sql = "truncate $tableName";
  77.         return $this->connect->query($sql);
  78.     }
  79.  
  80.     public function delete($tableName, $id)
  81.     {
  82.         $sql = "DELETE FROM {$tableName} WHERE id = {$id}";
  83.         return $this->connect->query($sql);
  84.     }
  85.  
  86.     public function getDataFromTable($tableName)
  87.     {
  88.         $sql = "SELECT * FROM $tableName";
  89.         $result = $this->connect->query($sql);
  90.         $products = array();
  91.         $count = 1;
  92.         if (mysqli_num_rows($result) > 0) {
  93.             while ($row = mysqli_fetch_assoc($result)) {
  94.                 $product = array(
  95.                     'name' => $row['name'],
  96.                     'price' => $row['price'],
  97.                     'description' => $row['description']
  98.                 );
  99.                 array_push($products, $product);
  100.                 $count++;
  101.             }
  102.         }
  103.         return $products;
  104.     }
  105. }
  106.  
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement