Advertisement
Guest User

DB Helper

a guest
Aug 29th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. class DBHelper{
  2.  
  3.     protected static $conn = null;
  4.      
  5.     public function __construct($dbname=DB_NAME,$host=DB_HOST,$user=DB_USER,$password=DB_PASSWORD){
  6.         $this->connect($this,$dbname,$host,$user,$password);
  7.     }
  8.  
  9.     function connect(&$context,$dbname,$host,$user,$password){
  10.         $connectionString = "mysql:host={$host};dbname={$dbname}";
  11.         try {
  12.             $context->conn = new PDO($connectionString,$user,$password);
  13.         } catch (PDOException $pe) {
  14.             die($pe->getMessage());
  15.         }
  16.     }
  17.  
  18.     public function __destruct() {
  19.         $this->conn = null;
  20.     }
  21. }
  22.  
  23. class DBUserHelper extends DBHelper{
  24.  
  25.     function accountLogin($username, $password){
  26.         $request = 'SELECT *
  27.            FROM user
  28.            WHERE username = :username AND
  29.                  password = :password';
  30.         $query = self::$conn->prepare($request);
  31.         $query->execute(
  32.             array(':username' => $username,
  33.                   ':password' => $password
  34.             ));
  35.         return $query;
  36.      }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement