-
-
Save flavius/5127446 to your computer and use it in GitHub Desktop.
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 | |
* Author: Flavius Aspra <[email protected]> | |
* | |
* This is just a start. No error handling or parameterization yet | |
*/ | |
class Database { | |
/** | |
* Database handler | |
*/ | |
protected $dbh; | |
/** | |
* Result of the last query | |
*/ | |
protected $result; | |
/** | |
* Constructs the PDO object. | |
* | |
* @param string $driver The PDO backend driver to use | |
* @param string $server The server | |
* @param string $db Database name | |
* @param string $username The username to connect with | |
* @param string $password The password | |
*/ | |
public function __construct($servertype, $server, $db, $username, $password) { | |
$this->setDbh(new PDO("$servertype:host=$server; dbname=$db", $username, $password)); | |
} | |
/** | |
* Get the PDO instance. | |
*/ | |
public function getDbh() { | |
return $this->dbh; | |
} | |
/** | |
* Set the PDO instance. | |
* | |
* Usually you don't call this, it's called at construction time. | |
* | |
* @param $pdo PDO The PDO instance | |
*/ | |
public function setDbh($pdo) { | |
$this->dbh = $x; | |
} | |
/** | |
* Execute a raw query. | |
* | |
* You should not be using this unless you know what you're doing. Instead, | |
* use prepared statements. | |
* | |
* @param string $query The SQL query | |
*/ | |
public function query($qry) { | |
$this->result = $this->dbh->query($qry); | |
$this->result->execute(); | |
} | |
/** | |
* Fetch the next row as a stdClass after executing a query. | |
*/ | |
public function fetchObject() { | |
$row = $this->result->fetch(PDO::FETCH_OBJ); | |
return $row; | |
} | |
/** | |
* Fetch the next row as a dictionary after executing a query. | |
*/ | |
public function fetchArray() { | |
$row = $this->result->fetch(PDO::FETCH_ASSOC); | |
return $row; | |
} | |
/** | |
* Fetch the next row as a list after executing a query. | |
*/ | |
public function fetchnum() { | |
$row = $this->result->fetch(PDO::FETCH_NUM); | |
return $row; | |
} | |
/** | |
* Get the number of rows after executing a query. | |
*/ | |
public function getRowCount() { | |
$count = count($this->result->fetchAll()); | |
return $count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment