Advertisement
Guest User

Untitled

a guest
Jun 21st, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.    
  5.     private static $instance = null;
  6.     private $results,
  7.             $sqlParams = array(),
  8.             $sql,
  9.             $db;
  10.  
  11.     private function __construct() {
  12.         try {
  13.             return $this->db = new PDO('mysql:host=127.0.0.1;dbname=cms', 'root', '');
  14.         } catch (PDOException $e) {
  15.             echo $e->getMessage();
  16.         }
  17.     }
  18.    
  19.     public static function getInstance() {
  20.         if(!isset(self::$instance)) {
  21.             self::$instance = new self;
  22.         }
  23.         return self::$instance;
  24.     }
  25.  
  26.     private function results($results) {
  27.         return $this->results = $results;
  28.     }
  29.  
  30.     public function getResults() {
  31.         return $this->results;
  32.     }
  33.  
  34.     public function select($fields = ['*'], $table = 'users') {
  35.         $fields = implode(', ', $fields);
  36.         $this->sql = "SELECT {$fields} FROM {$table}";
  37.         return $this;
  38.     }
  39.  
  40.     public function insert($table, $values) {
  41.         $fields = implode(', ', array_keys($values));
  42.         $this->sql = "INSERT INTO {$table} ($fields) VALUES (";
  43.         for($i = 0; count($values) > $i; $i++) {
  44.             $this->sql .= "?, ";
  45.         }
  46.         $this->sql = rtrim($this->sql, ', ');
  47.         $this->sql .= ")";
  48.     }
  49.  
  50.     public function update($table) {
  51.         $this->sql = "UPDATE {$table} SET ";
  52.  
  53.         return $this;
  54.     }
  55.  
  56.     public function delete() {
  57.         $this->sql = "DELETE FROM {$table} WHERE usersname = ?";
  58.     }
  59.  
  60.     public function set($fieldParams) {
  61.         array_push($this->sqlParams, array_values($fieldParams));
  62.         $fields = null;
  63.         foreach ($fieldParams as $field => $field_value) {
  64.             echo $field_value.'<br>';
  65.             $this->sql .= "{$field} = ?, ";
  66.         }
  67.         $this->sql = rtrim($this->sql, ', ');
  68.  
  69.         return $this;
  70.     }
  71.  
  72.     public function where($whereString, $whereParams, $operator = "AND") {
  73.         foreach ($whereParams as $value) {
  74.             array_push($this->sqlParams, $value);
  75.             echo $value.'<br>';
  76.         }
  77.         $this->sqlParams = array_push($this->sqlParams, $whereParams);
  78.         $where = implode(" {$operator} " , $whereString);
  79.         $this->sql .= " WHERE {$where}";
  80.  
  81.         return $this;
  82.     }
  83.  
  84.     public function order($order = ['id', 'DESC']) {
  85.         $order = implode(' ', $order);
  86.         $this->sql .= " ORDER BY {$order}";
  87.         return $this;
  88.     }
  89.  
  90.     public function limit($number = 1) {
  91.         $this->sql .= " LIMIT {$number}";
  92.         return $this;
  93.     }
  94.  
  95.     public function make() {
  96.         $sql = $this->sql;
  97.         $params = $this->sqlParams;
  98.         echo '<pre>' , print_r($this->sqlParams) , '</pre>';
  99.  
  100.         $query = $this->db->prepare($sql);
  101.  
  102.         for ($i = 0, $j = 1; count($params) > $i; $i++, $j++) {
  103.             $query->bindParam($j, $params[$i]);
  104.         }
  105.  
  106.         $query->execute();
  107.         $results = $query->fetchAll(PDO::FETCH_OBJ);
  108.  
  109.         return $this->results($results);
  110.     }
  111.  
  112.     public function getSqlParams() {
  113.         return $this->sqlParams;
  114.     }
  115.  
  116.     public function getSQL() {
  117.         return $this->sql;
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement