Last active
December 14, 2015 17:09
-
-
Save JeremyMorgan/5120382 to your computer and use it in GitHub Desktop.
Simple PDO Database 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 | |
/** | |
* File: database.class.php | |
* Super simple PDO class | |
* | |
* Author: Jeremy Morgan | |
* | |
* This is just a start. No error handling or parameterization yet | |
*/ | |
class Database { | |
public $dbh; // database object | |
private $result; // result set | |
private function getDbh() { | |
return $this->dbh; | |
} | |
private function setDbh($x) { | |
$this->dbh = $x; | |
} | |
#Constructor | |
public function __construct() { | |
#probaby best to store the credentials in a config file | |
$this->setDbh = new PDO('<SERVERTYPE>:host=<SERVER> ;dbname=<SOMEDB>', '<USERNAME>', '<PASSWORD>'); | |
} | |
#Execute a query | |
function query($qry) { | |
$this->result = $this->dbh->query($qry); | |
$this->result->execute(); | |
} | |
#Show result set as an object | |
function fetchobject() { | |
$row = $this->result->fetch(PDO::FETCH_OBJ); | |
return $row; | |
} | |
#Show result set as an array | |
function fetcharray() { | |
$row = $this->result->fetch(PDO::FETCH_ASSOC); | |
return $row; | |
} | |
#Show result set as a number | |
function fetchnum() { | |
$row = $this->result->fetch(PDO::FETCH_NUM); | |
return $row; | |
} | |
#Count total number of rows in database | |
function checkrowCount() { | |
$count = count($this->result->fetchAll()); | |
return $count; | |
} | |
} | |
?> |
Thank you, I have made the proper changes to it. I will be updating this Gist soon with a cleaner version soon.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line no: 27 setDbh is used as a constant/variable. Just above it is defined as a function.
Is this a bug?
and Line no. 33: Execute is not called as a function.