Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Database Class
- */
- class Database
- // Connect to the database
- {
- public $isConnected;
- protected $datab;
- public function __construct($username = DB_USER, $password = DB_PASS, $host = DB_HOST, $dbname = DB_NAME)
- {
- $this->isConnected = true;
- try {
- $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);
- $this->datab->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
- $this->datab->setAttribute ( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
- } catch (PDOException $e ) {
- $this->isConnected = false;
- throw new Exception( $e->getMessage () );
- }
- }
- //disconnect from database
- public function Disconnect()
- {
- $this->datab = null;
- $this->isConnected = false;
- }
- // get row
- public function getRow($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return $stmt->fetch();
- } catch (PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- // get rows
- public function getRows($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return $stmt->fetchAll();
- } catch (PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- // insert row
- public function insertRow($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return TRUE;
- } catch (PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- // update row
- public function updateRow($query, $params = []){
- $this->insertRow($query, $params);
- }
- // delete row
- public function deleteRow($query, $params = []){
- $this->insertRow($query, $params);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement