Advertisement
fabi0

Untitled

Nov 4th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 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 = array()) {
  17.         $return = array();
  18.         if (!is_object(self::$databaseInstance)) {
  19.             self::setInstance();
  20.         }
  21.         $sth = self::$databaseInstance->prepare($sql);
  22.         foreach ($params as $key => $value) {
  23.             $sth->bindParam(':' . $key, $value);
  24.         }
  25.  
  26.         $sth->execute();
  27.         while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
  28.             $return[] = $result;
  29.         }
  30.         return $return;
  31.     }
  32.  
  33.     private function setInstance() {
  34.         if (!is_object(self::$databaseInstance)) {
  35.             self::$databaseInstance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8', DB_USER, DB_PASSWORD);
  36.             self::$databaseInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  37.         }
  38.     }
  39.  
  40.     public static function getInstance() {
  41.         return self::$databaseInstance;
  42.     }
  43.  
  44. }
  45.  
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement