Advertisement
Guest User

Example PHP Singleton DB Class

a guest
Apr 18th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Db {
  2.     // The class instance
  3.     protected static $instance;
  4.    
  5.     // A connection to the db
  6.     private $connection;
  7.  
  8.     // Function to create and return an instance of the Db class
  9.     public static function getInstance(){
  10.         if( !(self::$instance) ){
  11.                     self::$instance = new self();          
  12.                 }
  13.                 return self::$instance;
  14.     }
  15.  
  16.     // Private class constructor prevents this class being instantiated other than by the getInstance() call
  17.     private function __construct(){
  18.         // Let's connect to the database here
  19.                 $config = parse_ini_file('./config.ini');
  20.             $this->connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
  21.            
  22.         // If connection was not successful, handle the error
  23.             if($this->connection === false) {
  24.                     // Handle error - notify administrator, log to a file, show an error screen, etc.
  25.                     throw new Exception('Database connection failed');
  26.             }
  27.     }
  28.  
  29.     /**
  30.         * Query the database
  31.         *
  32.         * @param $query The query string
  33.         * @return mixed The result of the mysqli::query() function
  34.         */
  35.         public function query($query) {
  36.             // Query the database
  37.             $result = $this->connection->query($query);
  38.             return $result;
  39.         }
  40.  
  41.         /**
  42.         * Fetch rows from the database (SELECT query)
  43.         *
  44.         * @param $query The query string
  45.         * @return bool False on failure / array Database rows on success
  46.         */
  47.         public function select($query) {
  48.             $rows = array();
  49.             $result = $this->query($query);
  50.             if($result === false) {
  51.                     return false;
  52.             }
  53.             while ($row = $result -> fetch_assoc()) {
  54.                     $rows[] = $row;
  55.             }
  56.             return $rows;
  57.         }
  58.  
  59.         /**
  60.         * Fetch the last error from the database
  61.         *
  62.         * @return string Database error message
  63.         */
  64.         public function error() {
  65.             return $this->connection->error;
  66.         }
  67.  
  68.         /**
  69.         * Quote and escape value for use in a database query
  70.         *
  71.         * @param string $value The value to be quoted and escaped
  72.         * @return string The quoted and escaped string
  73.         */
  74.         public function quote($value) {
  75.             return "'" . $this->connection->real_escape_string($value) . "'";
  76.         }
  77.    
  78.     // This prevents a bug in PHP to stop this class being cloned
  79.     private function __clone() {}
  80. }
  81.  
  82.  
  83.  
  84. try{
  85.     // Our database object
  86.     $db = Db::getInstance();    
  87.  
  88.     // Quote and escape form submitted values
  89.     $name = $db->quote($_POST['username']);
  90.     $email = $db->quote($_POST['email']);
  91.  
  92.     // Insert the values into the database
  93.     $result = $db->query("INSERT INTO `users` (`name`,`email`) VALUES (" . $name . "," . $email . ")");
  94. }
  95. catch (Exception $e){
  96.     // Handle error - notify administrator, log to a file, show an error screen, etc.
  97.     error_log($e->getMessage());
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement