Truffle
11-17-2011, 06:04 PM
I wrote this for a project I've been working on to help simplify database queries and keep the code clean. Also read that binding the parameters in your queries is great protection against injection hacks.
First, code examples
INSERT/UPDATE
$db = new Database();
$data = array( "John", "Smith" );
$sql = "INSERT INTO names ( firstname, lastname) VALUES( ?, ? )";
$db->insert( $data, $sql );
Selecting a single row
$data = array( 102 );
$sql = "SELECT * FROM names WHERE id = ? LIMIT 1";
$obj = $db->fetch_single_row( $data, $sql );
echo "{$obj->firstname} {$obj->lastname}";
Selecting Multiple Rows
$data = array( 'M', 27 );
$sql = "SELECT * FROM names WHERE sex = ? AND age = ?";
$result = $db->fetch_multiple_rows( $data, $sql );
while( $row = $result->fetch() ) {
echo "{$row->firstname} {$row->lastname}\n<br/>";
}
And finally the class
class Database {
private $host;
private $username;
private $password;
private $con;
private $pdo;
public function __construct( $database = "yourdb" )
{
$this->host = "localhost";
$this->username = $_SERVER['DB_USER'];
$this->password = $_SERVER['DB_PASSWORD'];
$conStr = "host={$this->host};dbname={$database}";
try {
$this->pdo = new PDO( "mysql:$conStr", $this->username, $this->password );
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
echo "error ". $e->getMessage();
}
}
public function fetch_single_row( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$sel = $this->pdo->prepare( $sql );
$sel->execute( $data );
$sel->setFetchMode( PDO::FETCH_OBJ );
$obj = $sel->fetch();
return $obj;
}
public function fetch_multiple_rows( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$sel = $this->pdo->prepare( $sql );
$sel->execute( $data );
$sel->setFetchMode( PDO::FETCH_OBJ );
return $sel;
}
public function insert( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$ins = $this->pdo->prepare( $sql );
$ins->execute( $data );
}
public function close()
{
$this->pdo = null;
}
public function __destruct()
{
$this->pdo = null;
}
}
First, code examples
INSERT/UPDATE
$db = new Database();
$data = array( "John", "Smith" );
$sql = "INSERT INTO names ( firstname, lastname) VALUES( ?, ? )";
$db->insert( $data, $sql );
Selecting a single row
$data = array( 102 );
$sql = "SELECT * FROM names WHERE id = ? LIMIT 1";
$obj = $db->fetch_single_row( $data, $sql );
echo "{$obj->firstname} {$obj->lastname}";
Selecting Multiple Rows
$data = array( 'M', 27 );
$sql = "SELECT * FROM names WHERE sex = ? AND age = ?";
$result = $db->fetch_multiple_rows( $data, $sql );
while( $row = $result->fetch() ) {
echo "{$row->firstname} {$row->lastname}\n<br/>";
}
And finally the class
class Database {
private $host;
private $username;
private $password;
private $con;
private $pdo;
public function __construct( $database = "yourdb" )
{
$this->host = "localhost";
$this->username = $_SERVER['DB_USER'];
$this->password = $_SERVER['DB_PASSWORD'];
$conStr = "host={$this->host};dbname={$database}";
try {
$this->pdo = new PDO( "mysql:$conStr", $this->username, $this->password );
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
echo "error ". $e->getMessage();
}
}
public function fetch_single_row( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$sel = $this->pdo->prepare( $sql );
$sel->execute( $data );
$sel->setFetchMode( PDO::FETCH_OBJ );
$obj = $sel->fetch();
return $obj;
}
public function fetch_multiple_rows( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$sel = $this->pdo->prepare( $sql );
$sel->execute( $data );
$sel->setFetchMode( PDO::FETCH_OBJ );
return $sel;
}
public function insert( $data, $sql )
{
if( $data !== null )
$data = array_values( $data ); // Incase it's an associative array
$ins = $this->pdo->prepare( $sql );
$ins->execute( $data );
}
public function close()
{
$this->pdo = null;
}
public function __destruct()
{
$this->pdo = null;
}
}