Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MYSQL {
- private static $_instance = null;
- private $_pdo,
- $_query,
- $_error = false,
- $_result,
- $_count = 0;
- private function __construct() {
- try {
- $this->_pdo = new PDO('mysql:host=' . CONFIG::getConfig('mysql/host') . ';dbname=' . CONFIG::getConfig('mysql/db'), CONFIG::getConfig('mysql/user'), CONFIG::getConfig('mysql/pass'));
- } catch(PDOException $e) {
- die($e->getMessage());
- }
- }
- public static function getInstance() {
- if(!isset(self::$_instance)) {
- self::$_instance = new MYSQL();
- }
- return self::$_instance;
- }
- public function query($sql, $params = array()) {
- $this->_error = false;
- $x = 1;
- if($this->_query = $this->_pdo->prepare($sql)) {
- if(count($params)) {
- foreach($params as $param) {
- $this->_query->bindValue($x, $param);
- $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 static function insert($table, $fields = array()) {
- if(count($fields)) {
- $keys = array_keys($fields);
- $values = null;
- $x = 1;
- foreach ($fields as $field) {
- $values .= "?";
- if($x < count($fields)) {
- $values .= ', ';
- }
- $x++;
- }
- $sql = "INSERT INTO " . $table . " (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
- if(!$this->query($sql, $fields)->error()) {
- return true;
- }
- }
- return false;
- }
- public function result() {
- return $this->_result;
- }
- public function error() {
- return $this->_error;
- }
- public function count() {
- return $this->_count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement