Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database{
- public $isConn;
- protected $datab;
- //CONNECT TO DB
- public function __construct($host = "", $user = "", $pass = "", $dbname = "", $options = [])
- {
- $this->isConn = TRUE;
- try {
- $this->datab = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass, $options);
- $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
- } catch (PDOException $e){
- throw new Exception($e->getMessage());
- }
- }
- //DISCONNECT FROM DB
- public function Disconnect()
- {
- $this->isConn = FALSE;
- $this->datab = NULL;
- }
- //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->Message());
- }
- }
- //UPDATE ROW
- public function updateRow($query, $params = [])
- {
- $this->insertRow($query, $params);
- }
- //DELETE ROW
- public function deleteRow($query, $params = [])
- {
- $this->insertRow($query, $params);
- }
- public function findAll($table, $params = [])
- {
- try {
- $stmt = $this->datab->prepare("SELECT * FROM $table");
- $stmt->execute($params);
- return $stmt->fetchAll();
- } catch(PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function findOne($table, $where, $params = [])
- {
- try {
- $stmt = $this->datab->prepare("SELECT * FROM $table WHERE $where = ? LIMIT 1");
- $stmt->execute($params);
- return $stmt->fetch();
- } catch(PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function search($table, $where, $params = [])
- {
- try {
- $stmt = $this->datab->prepare("SELECT * FROM $table WHERE $where LIKE ?");
- $stmt->execute($params);
- return $stmt->fetchAll(PDO::FETCH_OBJ);
- } catch(PDOException $e) {
- throw new Exception($e->getMessage());
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment