Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- private static $instance;
- private static $con;
- private static $customError;
- private static $queryObject;
- public static function init($host, $user, $pass, $db) {
- if(!isset(self::$instance)) {
- try {
- self::$instance = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass);
- //self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- self::$con = true;
- }
- catch (PDOException $e) {
- self::$customError = $e->getMessage();
- self::$con = false;
- }
- }
- }
- public static function isConnected() {
- return self::$con;
- }
- public static function select($table, $columns = '*', $where = NULL, $limit = NULL, $column2return = NULL)
- {
- $params = array();
- $sql = "SELECT ";
- if(is_array($columns))
- {
- for($i = 0; $i < count($columns); $i++)
- {
- $c = $columns[$i];
- $sql .= "`$c`";
- if($i != count($columns)-1)
- {
- $sql .= ", ";
- }
- }
- }
- else
- {
- if($columns == '*')
- {
- $sql .= '*';
- }
- else
- {
- $sql .= "`$columns`";
- }
- }
- $sql .= " FROM `$table`";
- if($where != NULL)
- {
- //Check if there are WHERE parameters
- if(!is_array($where))
- {
- echo 'Error in DB::select function: \'where\' parameter must be Array';
- return false;
- }
- //If WHERE is two values, example: user_id, 1
- //Put them into array for for loop
- if(!is_array($where[0]) && count($where) == 2)
- {
- $where = array($where);
- }
- $sql .= " WHERE";
- for($i = 0; $i < count($where); $i++)
- {
- //This current website
- $w = $where[$i];
- //Only add if array
- if(is_array($w))
- {
- //column = value
- //Example: user_id = 19
- if(count($w) == 2)
- {
- $sql .= " `$w[0]` = ?";
- $params[] = $w[1];
- }
- //column operator value
- //array("user_age", ">", 18)
- //Example: user_age > 18
- else if(count($w) == 3)
- {
- $sql .= " `$w[0]` $w[1] = ?";
- $params[] = $w[2];
- }
- //If not last item, append "AND" to query
- if($i != count($where)-1)
- {
- $sql = " AND";
- }
- }
- }
- }
- if($limit != NULL && is_int($limit))
- {
- $sql .= " LIMIT $limit";
- }
- $results = self::query($sql, $params);
- if(count($results) == 1)
- {
- $results = $results[0];
- }
- if($column2return != NULL)
- {
- if(is_array($column2return))
- {
- $arr = array();
- foreach($column2return as $col)
- {
- if(isset($results[$col]))
- {
- $arr[$col] = $results[$col];
- }
- }
- return $arr;
- }
- else
- {
- return $results[$column2return];
- }
- }
- else
- {
- return $results;
- }
- }
- public static function query($query, $params = array()) {
- try {
- $params = !is_array($params) ? array($params) : $params;
- $data = array();
- $is_select = strtoupper(substr($query,0,6)) == "SELECT" ? true : false;
- if(count($params) > 0) {
- $stmt = self::$instance->prepare($query);
- if(!$stmt)
- {
- return false;
- }
- $stmt->execute($params);
- self::$queryObject = $stmt;
- if($is_select)
- $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
- else {
- $data = true;
- }
- $stmt->closeCursor();
- }
- else {
- $res = self::$instance->query($query);
- self::$queryObject = $res;
- if(!$res)
- {
- return false;
- }
- else if($is_select)
- {
- foreach($res as $row)
- $data[] = $row;
- }
- else
- {
- $data = true;
- }
- }
- self::$customError = "";
- return $data;
- }
- catch(Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function getQueryObj()
- {
- return isset(self::$queryObject) ? self::$queryObject : false;
- }
- public static function getError() {
- if(strlen(self::$customError) > 0)
- {
- return self::$customError;
- }
- else
- {
- $err = self::$instance->errorInfo();
- return $err[count($err)-1];
- }
- }
- public static function setError($e) {
- self::$customError = $e;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement