Advertisement
fabi0

Untitled

Nov 4th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Description of dbClass
  5.  *
  6.  * @author fabi0
  7.  * $sql = "SELECT * FROM `lang` WHERE id=:ID";
  8.  * $params = array('ID' => '1');
  9.  * dbClass::query($sql, $params)
  10.  */
  11. final class dbClass {
  12.  
  13.     private static $databaseInstance = NULL;
  14.  
  15.     private function __construct() {
  16.        
  17.     }
  18.  
  19.     public static function query($sql, $params = NULL) {
  20.         if (mb_strlen($sql) == 0) {
  21.             throw new Exception('Empty SQL string.');
  22.         }
  23.         $return = array();
  24.         if (!is_object(self::$databaseInstance)) {
  25.             self::setInstance();
  26.         }
  27.         $sth = self::$databaseInstance->prepare($sql);
  28.         if (is_array($params)) {
  29.             foreach ($params as $key => $value) {
  30.                 if ($value['data_type'] == 'PDO::PARAM_INT') {
  31.                     $sth->bindParam(':' . $key, $value['variable'], PDO::PARAM_INT);
  32.                 }
  33.                 if ($value['data_type'] == 'PDO::PARAM_STR') {
  34.                     $sth->bindParam(':' . $key, $value['variable'], PDO::PARAM_STR);
  35.                 }
  36.             }
  37.         }
  38.         $sth->execute();
  39.         while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
  40.             $return[] = $result;
  41.         }
  42.         return $return;
  43.     }
  44.  
  45.     private function setInstance() {
  46.         if (!is_object(self::$databaseInstance)) {
  47.             self::$databaseInstance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8', DB_USER, DB_PASSWORD);
  48.             self::$databaseInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  49.         }
  50.     }
  51.  
  52.     public static function getInstance() {
  53.         return self::$databaseInstance;
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement