Created
November 14, 2017 01:13
-
-
Save spacebit-official/a6cf0f59f5da31f0fa3d530058897c80 to your computer and use it in GitHub Desktop.
pdo.class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment