Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.     /**
  5.      * @var Database
  6.      */
  7.     protected static $_dbInstance = null;
  8.  
  9.     /**
  10.      * @var PDO
  11.      */
  12.     protected $_dbHandle;
  13.  
  14.     /**
  15.      * @return Database
  16.      */
  17.     public static function getInstance() {
  18.         $username ='root';
  19.         $password = 'PASS';
  20.         $host = 'localhost';
  21.         $dbName = 'DBNAME';
  22.  
  23.         if(self::$_dbInstance === null) { //checks if the PDO exists
  24.             // creates new instance if not, sending in connection info
  25.             self::$_dbInstance = new self($username, $password, $host, $dbName);
  26.         }
  27.  
  28.         return self::$_dbInstance;
  29.     }
  30.  
  31.     /**
  32.      * @param $username
  33.      * @param $password
  34.      * @param $host
  35.      * @param $database
  36.      */
  37.     private function __construct($username, $password, $host, $database) {
  38.         try {
  39.             $this->_dbHandle = new PDO("mysql:host=$host;dbname=$database",  $username, $password); // creates the database handle with connection info
  40.             //$this->_dbHandle = new PDO('mysql:host=' . $host . ';dbname=' . $database,  $username, $password); // creates the database handle with connection info
  41.  
  42.         }
  43.         catch (PDOException $e) { // catch any failure to connect to the database
  44.             echo $e->getMessage();
  45.         }
  46.     }
  47.  
  48.     /**
  49.      * @return PDO
  50.      */
  51.     public function getdbConnection() {
  52.         return $this->_dbHandle; // returns the PDO handle to be used                                        elsewhere
  53.     }
  54.  
  55.     public function __destruct() {
  56.         $this->_dbHandle = null; // destroys the PDO handle when no longer needed                                        longer needed
  57.     }
  58.  
  59.     public function addUser($username,$password){
  60.         //$hashed = $this->hashPassword($password);
  61.         // $sqlQuery = 'INSERT INTO Users (`email`, `password`) VALUES ($username, $hashed)';
  62.         $sqlQuery = 'INSERT INTO Users (`email`) VALUES ($username)';
  63.  
  64.         echo $username;
  65.         $statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
  66.         $statement->execute(); // execute the PDO statement
  67.         return true;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement