Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Models;
- class DB {
- private static $_instance = NULL;
- private $_pdo,
- $_query,
- $_error = FALSE,
- $_result,
- $_count = 0;
- private function __construct() {
- try {
- $this->_pdo = new \PDO('mysql:host=' . \Models\Config::get('mysql/host') . ';dbname=' . \Models\Config::get('mysql/db'), \Models\Config::get('mysql/username'), \Models\Config::get('mysql/password'));
- } catch (\PDOException $e) {
- die($e->getMessage());
- }
- }
- public static function getInstance() {
- if (!isset(self::$_instance)) {
- self::$_instance = new \Models\DB();
- }
- return self::$_instance;
- }
- public function query($sql, $params = array()) {
- $this->_error = FALSE;
- if ($this->_query = $this->_pdo->prepare($sql)) {
- $x = 1;
- if (count($params)) {
- foreach ($params as $params) {
- $this->_query->bindParam($x, $params);
- $x++;
- }
- }
- if ($this->_query->execute()) {
- $this->_result = $this->_query->fetchAll(\PDO::FETCH_OBJ);
- $this->_count = $this->_query->rowCount();
- } else {
- $this->_error = TRUE;
- }
- }
- return $this;
- }
- public function error() {
- return $this->_error;
- }
- public function getResult() {
- return $this->_result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement