Skip to content

Instantly share code, notes, and snippets.

@kfriend
Last active August 11, 2016 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfriend/4a4b36d5c3143b5feb12 to your computer and use it in GitHub Desktop.
Save kfriend/4a4b36d5c3143b5feb12 to your computer and use it in GitHub Desktop.
PHP: Very simple PDO DB wrapper
<?php
class Db
{
protected $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function select($query, array $arguments = array())
{
try {
$stmt = $this->connection->prepare($query);
$stmt->execute($arguments);
} catch(Exception $e) {
return null;
}
$stmt->setFetchMode(PDO::FETCH_INTO, new StdClass);
return $stmt->fetchAll();
}
public function insert($query, array $data = array())
{
try {
$stmt = $this->connection->prepare($query);
return $stmt->execute($data);
} catch(Exception $e) {
return null;
}
}
public function getConnection()
{
return $this->connection;
}
public function setConnection(PDO $connection)
{
$this->connection = $connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment