Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- public $isConn;
- protected $datab;
- //connect db
- public function __construct($username = "root", $password = "", $host = "localhost", $dbname = 'copart', $options = []){
- $this->isConn = TRUE;
- try {
- $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
- $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- //throw new Exception($e->getMessage());
- echo $e->getMessage();
- }
- }
- //disconnect
- public function Disconnect(){
- $this->datab = NULL;
- $this->isConn = FALSE;
- }
- //get row
- public function getRow($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return $stmt->fetch();
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
- //get rows
- public function getRows($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return $stmt->fetchAll();
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
- //insert row
- public function insertRow($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return TRUE;
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
- //update row
- public function updateRow($query, $params = []){
- $this->insertRow($query,$params);
- }
- //delete row
- public function deleteRow($query, $params = []){
- $this->insertRow($query,$params);
- }
- }
Add Comment
Please, Sign In to add comment