Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- /**
- * @var Database
- */
- protected static $_dbInstance = null;
- /**
- * @var PDO
- */
- protected $_dbHandle;
- /**
- * @return Database
- */
- public static function getInstance() {
- $username ='root';
- $password = 'PASS';
- $host = 'localhost';
- $dbName = 'DBNAME';
- if(self::$_dbInstance === null) { //checks if the PDO exists
- // creates new instance if not, sending in connection info
- self::$_dbInstance = new self($username, $password, $host, $dbName);
- }
- return self::$_dbInstance;
- }
- /**
- * @param $username
- * @param $password
- * @param $host
- * @param $database
- */
- private function __construct($username, $password, $host, $database) {
- try {
- $this->_dbHandle = new PDO("mysql:host=$host;dbname=$database", $username, $password); // creates the database handle with connection info
- //$this->_dbHandle = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password); // creates the database handle with connection info
- }
- catch (PDOException $e) { // catch any failure to connect to the database
- echo $e->getMessage();
- }
- }
- /**
- * @return PDO
- */
- public function getdbConnection() {
- return $this->_dbHandle; // returns the PDO handle to be used elsewhere
- }
- public function __destruct() {
- $this->_dbHandle = null; // destroys the PDO handle when nolonger needed longer needed
- }
- public function addUser($username,$password){
- //$hashed = $this->hashPassword($password);
- // $sqlQuery = 'INSERT INTO Users (`email`, `password`) VALUES ($username, $hashed)';
- $sqlQuery = 'INSERT INTO Users (`email`) VALUES ($username)';
- echo $username;
- $statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
- $statement->execute(); // execute the PDO statement
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement