Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- private static $instance;
- private $dbh;
- private $error;
- private $stmt;
- public static function getInstance() {
- $host = '127.0.0.1';
- $user = 'user';
- $pass = 'password';
- $dbname = 'dbname';
- $port = '3306';
- if (!self::$instance) {
- // If no instance then make one
- self::$instance = new self($host, $user, $pass, $dbname, $port);
- }
- return self::$instance;
- }
- private function __construct($host, $user, $pass, $dbname, $port) {
- $dsn = 'mysql:host=' . $host . ';port=' . $port . ';dbname=' . $dbname;
- // Set options
- $options = array(
- PDO::ATTR_PERSISTENT => false,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
- );
- // Create a new PDO instanace
- try {
- $this->dbh = new PDO($dsn, $user, $pass, $options);
- }
- // Catch any errors
- catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- //error_log($log, 3, LOG_PATH.'mysql_error.log');
- //throw new Exception("DBError: " . $e->getMessage());
- }
- }
- private function bindType($value = null) {
- switch (true) {
- case is_int($value):
- $type = PDO::PARAM_INT;
- break;
- case is_bool($value):
- $type = PDO::PARAM_BOOL;
- break;
- case is_null($value):
- $type = PDO::PARAM_NULL;
- break;
- default:
- $type = PDO::PARAM_STR;
- }
- return $type;
- }
- public function select($sql, array $bind = []) {
- $this->stmt = $this->dbh->prepare($sql);
- $result = false;
- if (count($bind) > 0) {
- foreach ($bind as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- }
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $data = array();
- while ($row = $this->stmt->fetch(\PDO::FETCH_ASSOC)) {
- $data[] = $row;
- }
- $result = new stdClass();
- $result->row = (isset($data[0]) ? $data[0] : array());
- $result->rows = $data;
- $result->num_rows = $this->stmt->rowCount();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $result;
- }
- public function insert($table, array $data = []) {
- $columnString = implode(',', array_keys($data));
- $valueString = ':' . implode(',:', array_keys($data));
- $sql = "INSERT INTO {$table} ({$columnString}) VALUES ({$valueString})";
- $this->stmt = $this->dbh->prepare($sql);
- if (count($data) > 0) {
- foreach ($data as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- }
- $return = 0;
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $return = $this->dbh->lastInsertId();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $return;
- }
- public function insertIgnore($table, array $data = []) {
- $columnString = implode(',', array_keys($data));
- $valueString = ':' . implode(',:', array_keys($data));
- $sql = "INSERT IGNORE INTO {$table} ({$columnString}) VALUES ({$valueString})";
- $this->stmt = $this->dbh->prepare($sql);
- if (count($data) > 0) {
- foreach ($data as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- }
- $return = 0;
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $return = $this->dbh->lastInsertId();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $return;
- }
- public function update($table, array $column, $where, array $bind) {
- $_column = [];
- foreach ($column as $key => $val) {
- $_column[] = $key . " = :" . $key;
- }
- $_where = '';
- if (!empty($where)) {
- $_where = ' WHERE ' . $where;
- }
- $sql = "UPDATE {$table} SET " . implode(', ', $_column) . " {$_where}";
- $this->stmt = $this->dbh->prepare($sql);
- foreach ($column as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- foreach ($bind as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- $return = 0;
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $return = $this->stmt->rowCount();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $return;
- }
- public function updateRaw($table, $column, $where, array $bind) {
- $_where = '';
- if (!empty($where)) {
- $_where = ' WHERE ' . $where;
- }
- $sql = "UPDATE {$table} SET {$column} {$_where}";
- $this->stmt = $this->dbh->prepare($sql);
- foreach ($bind as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- $return = 0;
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $return = $this->stmt->rowCount();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $return;
- }
- public function delete($table, $where, array $bind) {
- $sql = "DELETE FROM {$table} WHERE {$where}";
- $this->stmt = $this->dbh->prepare($sql);
- foreach ($bind as $key => $val) {
- $this->stmt->bindValue(':' . $key, $val, $this->bindType($val));
- }
- $return = 0;
- try {
- if ($this->stmt && $this->stmt->execute()) {
- $return = $this->stmt->rowCount();
- }
- } catch (PDOException $e) {
- $log .= "\nSQL TIME: " . date('c');
- $log .= "\nQuery: CONNECTING DB";
- $log .= "\nERROR: " . $e->getMessage();
- $log .= "\nERROR CODE: " . $e->getCode();
- $log .= "\n----------------------------------------------------------";
- error_log($log, 3, LOG_PATH . 'mysql_error.log');
- //throw new Exception('DBError: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
- }
- return $return;
- }
- public function getConnection() {
- return $this->dbh;
- }
- public function lastInsertId() {
- return $this->dbh->lastInsertId();
- }
- public function beginTransaction() {
- return $this->dbh->beginTransaction();
- }
- public function endTransaction() {
- return $this->dbh->commit();
- }
- public function cancelTransaction() {
- return $this->dbh->rollBack();
- }
- public function debugDumpParams() {
- return $this->stmt->debugDumpParams();
- }
- public function __destruct() {
- $this->dbh = null;
- }
- private function __clone() {}
- }
- //$result = $db->select("SELECT * FROM `store_media` WHERE sm_id = :sm_id",array("sm_id"=>"7"));
- //$result = $db->select("store_media",'*',"",[]);
- //$result = $db->insert('store_media',['sm_store_id'=>'22', 'sm_parent_id'=>'33', 'sm_name'=>'44', 'sm_type'=>'55', 'sm_path'=>'66', 'sm_width'=>'77', 'sm_height'=>'88', 'sm_isfile'=>'99', 'sm_creation_date'=>date('Y-m-d H:i:s')]);
- //$result = $db->update('store_media',['sm_name'=>'21'],'sm_id = :sm_id',['sm_id'=>1]);
- #$result = $db->delete('store_media','sm_id = :sm_id',['sm_id'=>1]);
- #print_r($result);
Add Comment
Please, Sign In to add comment