Advertisement
fabi0

Untitled

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