Skip to content

Instantly share code, notes, and snippets.

@puncoz
Created December 8, 2015 15:50
Show Gist options
  • Save puncoz/6463ee57790d0f8acda1 to your computer and use it in GitHub Desktop.
Save puncoz/6463ee57790d0f8acda1 to your computer and use it in GitHub Desktop.
PDO database abstraction layer that you can use in your applications to allow for future flexibility in your database choice and protect you from SQL Injection.
<?php
// Define configuration
// May Include this in database configuration file
define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB_NAME", "database");
/*
PDO Database Class
*/
class PDO_Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
// Constructor
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
// Prepare
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
// Bind
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute
public function execute(){
return $this->stmt->execute();
}
// Result Set
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Single
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
// Row Count
public function rowCount(){
return $this->stmt->rowCount();
}
// Last Insert Id
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
/* TRANSACTIONS */
// To begin a transaction
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
// To end a transaction and commit your changes
public function endTransaction(){
return $this->dbh->commit();
}
// To cancel a transaction and roll back your changes
public function cancelTransaction(){
return $this->dbh->rollBack();
}
// Debug Dump Parameters
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
/* Example: Using your PDO class */
// Instantiate database.
$database = new PDO_Database();
/* [To Insert a new record] */
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
// bind data
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');
// execute
$database->execute();
//fetch last insert id
echo $database->lastInsertId();
/* [To Select a single row] */
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');
$database->bind(':fname', 'Jenny');
$row = $database->single();
/* [To Select multiple rows] */
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');
$database->bind(':lname', 'Smith');
$rows = $database->resultset();
// display the number of records returned.
echo $database->rowCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment