Here is a simple DB class used as a simple PDO wrapper.
Comes with easy options for CRUD interaction and feels similar to Eloquent DB system.
Class:
Code: Select all
<?php
class DB
{
private static $_instance = null;
private $_pdo,
$_query,
$_results,
$_error = false,
$_count = 0;
public function __construct($host, $username, $password, $db)
{
try
{
$this->_pdo = new PDO('mysql:host=' . $this->_host . ';dbname=' . $this->_db, $this->_username, $this->_password);
}
catch(PDOException $ex)
{
die($ex->getMessage());
}
}
public static function getInstance()
{
if(!isset(self::$_instance))
self::$_instance = new DB();
return self::$_instance;
}
public function query($sql, $params = array())
{
$this->_error = false;
if($this->_quert = $this->_pdo->prepare($sql))
{
if(count($params))
{
$x = 1;
foreach($params as $param)
{
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute())
{
$this->_results = $this->_query->fetch(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else
{
$this->_error = true;
}
}
return $this;
}
public function action($action, $table, $where = array(), $order = "ASC")
{
if(count($where) === 3)
{
$ops = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$op = $where[1];
$val = $where[2];
if(in_array($op, $ops))
{
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($val))->error())
{
return $this;
}
}
}
if(count($where) === 0)
{
$sql = "{$action} FROM {$table}";
if(!$this->query($sql)->error())
{
return $this;
}
}
return false;
}
public function insert($table, $fields = array())
{
if(count($fields))
{
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field)
{
$values .= "?";
if($x < count($fields))
{
$values .= ", ";
}
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
if(!$this->_query($sql, $fields)->error())
{
return true;
}
}
return false;
}
public function update($table, $options, $fields)
{
$set = '';
$x = 1;
$editing = $options[0];
$val = $options[1];
foreach($fields as $name => $value)
{
$set .= "{$name} = ?";
if($x < count($fields))
{
set .= ", ";
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE {$editing} = {$val}";
if(!$this->query($sql, $fields)->error())
{
return true;
}
return false;
}
public function delete($table, $where)
{
return $this->action("DELETE ", $table, $where);
}
public function get()
{
return $this->action("SELECT *", $table, $where);
}
public function error()
{
return $this->_error;
}
public function count()
{
return $this->_count;
}
public function results($index = '')
{
if(is_numeric($index))
{
return $this->single_result($index);
}
else
{
return $this->_results;
}
}
private function single_result($index)
{
return $this->_results[$index];
}
public function first()
{
return $this->_results[0];
}
}
And here are a few examples of use:
Code: Select all
<?php
require_once 'DB.php';
$db = DB::getInstance();
// Read
$get = $db->get('mytable', array('username', '=', 'bob1'));
// returns PDO object of Bob.
if($get->count() > 0)
{
$bob = $get->first();
echo "Hello, " . $bob->first_name;
}
// Create
$ins = $db->insert('mytable', array(
'username' => 'bob1',
'password' => 'iambob',
'first_name' => 'Bob',
'last_name' => 'Bobbykins'
));
// Update
$up = $db->update('mytable', array('username', 'bob1'), array(
'password' => 'iambobnewpassword'
));
// Destroy
$del = $db->delete('mytable', array('username', '=', 'bob1'));
As stated before, this is a very simple wrapper, and I don't really recommend using it in a production site, but is a good start for learning about it.