SHARE
TWEET

Untitled

a guest Jul 20th, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Database
  4. {
  5.     function __construct()
  6.     {
  7.         $host = 'localhost';
  8.         $port = 3306;
  9.         $database = 'test';
  10.         $username = 'root';
  11.         $password = '';
  12.  
  13.         $dsn = "mysql:host=$host;port=$port;dbname=$database";
  14.         $this->db = new PDO($dsn, $username, $password);
  15.     }
  16.  
  17.     function sql($sql)
  18.     {
  19.         $this->db->query($sql);
  20.     }
  21.    
  22.     function select($select)
  23.     {
  24.         $sql = "SELECT " . (isset($select['SELECT']) ? $select['SELECT'] : "*") . " FROM ";
  25.  
  26.         if (!isset($select['FROM'])) die("FROM not set");
  27.  
  28.         if (is_array($select['FROM']))
  29.             foreach ($select['FROM'] as $where => $whereas)
  30.                 $sql .= "$where AS $whereas, ";
  31.         else $sql .= $select['FROM'] . " ";
  32.  
  33.         if (is_array($select['WHERE']))
  34.         {
  35.             $sql .= "WHERE ";
  36.             foreach ($select['WHERE'] as $where => $squals)
  37.             {
  38.                 $sql .= "$where = ? AND ";
  39.                 $where_ary[] = $equals;
  40.             }
  41.         }
  42.         else if (isset($select['WHERE']))
  43.         {
  44.             $sql .= "WHERE " . $select['WHERE'] . " ";
  45.         }
  46.  
  47.         $sql .= (isset($select['ORDER']) ? "ORDER BY " . $select['ORDER'] . " " : "") . (isset($select['LIMIT']) ? "LIMIT " . $select['LIMIT'] . " " : "");
  48.  
  49.         $statement = $this->db->prepare($sql);
  50.         $statement->execute($where_ary);
  51.         return $statement;
  52.     }
  53.  
  54.     function select_row($select)
  55.     {
  56.         $statement = $this->select($select) or die("lol");
  57.  
  58.         while ($result = $statement->fetch())
  59.             return $result;
  60.     }
  61.  
  62.     function insert($insert)
  63.     {
  64.         $i=0;
  65.         foreach (array_keys($insert['VALUES']) as $value)
  66.         {
  67.             $value_keys .= $value;
  68.             if ($i < count($insert['VALUES'])-1) $value_keys .= ", ";
  69.             $i++;
  70.         }
  71.  
  72.         $values_ary = array_values($insert['VALUES']);
  73.         $values = "?";
  74.         for ($i=0; $i<count($insert['VALUES'])-1; $i++)
  75.             $values .= ", ?";
  76.  
  77.         $sql = "INSERT INTO " . $insert['TABLE'] . " ($value_keys) VALUES ($values);";
  78.  
  79.         $statement = $this->db->prepare($sql);
  80.         $statement->execute($values_ary);
  81.         return $statement;
  82.     }
  83.  
  84.     function update($update)
  85.     {
  86.         $sql = "UPDATE " . $update['TABLE'] . " SET ";
  87.  
  88.         if (is_array($update['VALUES']))
  89.         {
  90.             $i=0;
  91.             foreach($update['VALUES'] as $value => $to)
  92.             {
  93.                 $sql .= "$value = ?";
  94.                 if ($i < count($update['VALUES'])-1) $sql .= ", ";
  95.                 $update_ary[] = $to;
  96.                 $i++;
  97.             }
  98.         }
  99.         else $sql .= $update['VALUES'] . " ";
  100.  
  101.         $sql .= " WHERE ";
  102.  
  103.         if (is_array($update['WHERE']))
  104.         {
  105.             $i=0;
  106.             foreach($update['WHERE'] as $value => $is)
  107.             {
  108.                 $sql .= "$value = ?";
  109.                 if ($i < count($update['WHERE'])-1) $sql .= ", ";
  110.                 $update_ary[] = $is;
  111.                 $i++;
  112.             }
  113.         }
  114.         else $sql .= $update['WHERE'] . " ";
  115.  
  116.         $statement = $this->db->prepare($sql);
  117.         $statement->execute($update_ary);
  118.         return $statement;
  119.     }
  120.  
  121.     function delete($delete)
  122.     {
  123.         $sql = "DELETE FROM " . $delete['FROM'] . " WHERE ";
  124.  
  125.         if (is_array($delete['WHERE']))
  126.         {
  127.             $i=0;
  128.             foreach($delete['WHERE'] as $value => $is)
  129.             {
  130.                 $sql .= "$value = ?";
  131.                 if ($i<count($delete['WHERE'])-1) $sql .= ", ";
  132.                 $values_ary[] = $is;
  133.                 $i++;
  134.             }
  135.             echo $sql . print_r($values_ary);
  136.             $statement = $this->db->prepare($sql);
  137.             $statement->execute($values_ary);
  138.             return $statement;
  139.         }
  140.        
  141.         $sql .= $delete['WHERE'];
  142.         $this->sql($sql);
  143.     }
  144.  
  145.     function clean($table)
  146.     {
  147.         $this->sql("DELETE FROM " . $table);
  148.     }
  149.  
  150.     function drop($table)
  151.     {
  152.         $this->sql("DROP TABLE " . $table);
  153.     }
  154. }
  155.  
  156. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top