Skip to content

Instantly share code, notes, and snippets.

@greenspray
Forked from luckyshot/db.php
Created January 28, 2016 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenspray/b4996407d5ae4495d30c to your computer and use it in GitHub Desktop.
Save greenspray/b4996407d5ae4495d30c to your computer and use it in GitHub Desktop.
PHP/MySQL (PDO) method with named parameters
/*
PHP/MySQL (PDO) method with named parameters
---------------------------------------------
https://gist.github.com/luckyshot/9477105
$config = [
'mysql' => [
'database' => 'database',
'user' => 'root',
'pass' => 'root'
],
];
$db = new DB(
$config['mysql']['user'],
$config['mysql']['pass'],
$config['mysql']['database']
);
$dbdebug = new DBdebug();
$select = $db->query("SELECT * FROM items WHERE id = :id")
->bind(':id', $id)
->select();
$single = $db->query("SELECT * FROM items WHERE id = :id LIMIT 1")
->bind(':id', $id)
->single();
// Returns the inserted ID
$insert = $db->query("INSERT INTO items SET
aaa = :aaa,
bbb = :bbb
")
->bind(':aaa', $aaa )
->bind(':bbb', $bbb )
->insert();
$update = $db->query("UPDATE items SET
aaa = :aaa
WHERE id = :id;
")
->bind(':aaa', $aaa )
->bind(':id', $data['id'])
->execute();
$delete = $db->query("DELETE FROM items
WHERE id = :id
LIMIT 1;
")
->bind(':id', $data['id'])
->execute();
*/
class DB {
private $dbh;
private $stmt;
public function __construct($user, $pass, $dbname) {
$this->dbh = new PDO(
"mysql:host=localhost;dbname=$dbname",
$user,
$pass,
array( PDO::ATTR_PERSISTENT => true )
);
$this->query("SET NAMES 'utf8';");
$this->execute();
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
return $this;
}
public function bind($pos, $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($pos, $value, $type);
return $this;
}
public function execute() {
return $this->stmt->execute();
}
// Same as execute() but returns ID
public function insert() {
$this->stmt->execute();
return $this->dbh->lastInsertId();
}
public function select( $fetch_style = null ) {
$this->execute();
return $this->stmt->fetchAll( PDO::FETCH_ASSOC );
}
public function single( $fetch_style = null ) {
$this->execute();
return $this->stmt->fetch( PDO::FETCH_ASSOC );
}
}
/*
A clone of DB to help in debugging
echo $dbdebug->query("SELECT * FROM items WHERE id = :id")
->bind(':id', $id)
->select();
*/
class DBdebug {
private $q;
public function query( $q )
{
$this->q = $q;
return $this;
}
public function bind( $what, $with, $type = null )
{
if ( is_int( $with ) )
{
$this->q = str_replace( $what, $with, $this->q );
}
else
{
$this->q = str_replace( $what, "'" . $with . "'", $this->q );
}
return $this;
}
public function execute() { return $this->q; }
public function insert() { return $this->q; }
public function select() { return $this->q; }
public function single() { return $this->q; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment