Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- private static $instance = null;
- private $results,
- $sqlParams = array(),
- $sql,
- $db;
- private function __construct() {
- try {
- return $this->db = new PDO('mysql:host=127.0.0.1;dbname=cms', 'root', '');
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
- public static function getInstance() {
- if(!isset(self::$instance)) {
- self::$instance = new self;
- }
- return self::$instance;
- }
- private function results($results) {
- return $this->results = $results;
- }
- public function getResults() {
- return $this->results;
- }
- public function select($fields = ['*'], $table = 'users') {
- $fields = implode(', ', $fields);
- $this->sql = "SELECT {$fields} FROM {$table}";
- return $this;
- }
- public function insert($table, $values) {
- $fields = implode(', ', array_keys($values));
- $this->sql = "INSERT INTO {$table} ($fields) VALUES (";
- for($i = 0; count($values) > $i; $i++) {
- $this->sql .= "?, ";
- }
- $this->sql = rtrim($this->sql, ', ');
- $this->sql .= ")";
- }
- public function update($table) {
- $this->sql = "UPDATE {$table} SET ";
- return $this;
- }
- public function delete() {
- $this->sql = "DELETE FROM {$table} WHERE usersname = ?";
- }
- public function set($fieldParams) {
- array_push($this->sqlParams, array_values($fieldParams));
- $fields = null;
- foreach ($fieldParams as $field => $field_value) {
- echo $field_value.'<br>';
- $this->sql .= "{$field} = ?, ";
- }
- $this->sql = rtrim($this->sql, ', ');
- return $this;
- }
- public function where($whereString, $whereParams, $operator = "AND") {
- foreach ($whereParams as $value) {
- array_push($this->sqlParams, $value);
- echo $value.'<br>';
- }
- $this->sqlParams = array_push($this->sqlParams, $whereParams);
- $where = implode(" {$operator} " , $whereString);
- $this->sql .= " WHERE {$where}";
- return $this;
- }
- public function order($order = ['id', 'DESC']) {
- $order = implode(' ', $order);
- $this->sql .= " ORDER BY {$order}";
- return $this;
- }
- public function limit($number = 1) {
- $this->sql .= " LIMIT {$number}";
- return $this;
- }
- public function make() {
- $sql = $this->sql;
- $params = $this->sqlParams;
- echo '<pre>' , print_r($this->sqlParams) , '</pre>';
- $query = $this->db->prepare($sql);
- for ($i = 0, $j = 1; count($params) > $i; $i++, $j++) {
- $query->bindParam($j, $params[$i]);
- }
- $query->execute();
- $results = $query->fetchAll(PDO::FETCH_OBJ);
- return $this->results($results);
- }
- public function getSqlParams() {
- return $this->sqlParams;
- }
- public function getSQL() {
- return $this->sql;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement