Created
October 4, 2012 16:28
-
-
Save cam-gists/3834774 to your computer and use it in GitHub Desktop.
PHP: PDO Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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