Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created October 4, 2012 16:28
Show Gist options
  • Select an option

  • Save cam-gists/3834774 to your computer and use it in GitHub Desktop.

Select an option

Save cam-gists/3834774 to your computer and use it in GitHub Desktop.
PHP: PDO Wrapper
<?php
/**
* PDO Wrapper Class
* @author Christopher A. Moore <[email protected]>
*
* @refs :
* http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
* @test
*/
class PdoWrapper {
private $dbh;
public $sql;
/**
* Setup the Object
* I know db creds are hard coded for now
*/
function __construct() {
$this->dbh = new PDO( "mysql:host=localhost;dbname=dbname", 'root', 'root' );
$this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
/**
* Straight Queries / Preparin Staements
*/
function call() {
try {
$this->sth = $this->dbh->prepare( $this->sql );
$this->sth = $this->dbh->query( $this->sql );
$this->sth->setFetchMode( PDO::FETCH_ASSOC );
while ( $row = $this->sth->fetch() ) {
$data[] = $row;
}
return json_encode( $data[0] );
}
catch( PDOException $e ) {
echo $e->getMessage();
}
}
}
$pdo = new PdoWrapper();
//new Orders Today
$pdo->sql = $sql['title'];
$data = $pdo->call();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment