Created
February 9, 2012 23:02
-
-
Save alexburner/1784086 to your computer and use it in GitHub Desktop.
building a MySQL wrapper
This file contains 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
class MySQLWrapper { | |
private $db = array( | |
'host' => null, | |
'name' => null, | |
'user' => null, | |
'pass' => null, | |
'pdo' => null | |
); | |
function __construct( $host, $name, $user, $pass ) { | |
$this->db[ 'host' ] = $host; | |
$this->db[ 'name' ] = $name; | |
$this->db[ 'user' ] = $user; | |
$this->db[ 'pass' ] = $pass; | |
$this->buildPDO(); | |
} | |
public function setConfig( $args ) { | |
if ( !is_array( $args ) ) return; | |
$keys = array_keys( $db ); | |
foreach ( $keys as $key ) if ( isset( $args[ $key ] ) ) $this->db[ $key ] = $args[ $key ]; | |
$this->buildPDO(); | |
} | |
private function buildPDO() { | |
$this->db[ 'pdo' ] = new PDO( | |
'mysql:host=' . $this->db[ 'host' ] . ';' . | |
'dbname=' . $this->db[ 'name' ], | |
$this->db[ 'user' ], | |
$this->db[ 'pass' ] | |
); | |
} | |
public function query( $query ) { | |
$statement = $this->db[ 'pdo' ]->query( $query ); | |
$rows = array(); | |
while( $rows[] = $statement->fetch( PDO::FETCH_ASSOC ) ) continue; | |
return $rows; | |
} | |
public function getRowsByID( $table, $column, $id ) { | |
return $this->query( 'SELECT * FROM ' . $table . ' WHERE ' . $column . ' = ' $id ); | |
} | |
public function updateRowByID( $table, $column, $id, $data ) { | |
// TODO generate dataString | |
$result = $this->query( 'UPDATE ' . $table . ' SET ' . $dataString . ' WHERE ' . $column . ' = ' $id ); | |
print_r( $result ); | |
} | |
public function insertRowByID( $table, $column, $id, $data ) {} | |
public function setRowByID( $table, $column, $id, $data ) { | |
$row = $this->getRows( $table, $column, $id ); | |
if ( count( $row ) ) return $this->updateRow( $table, $column, $id, $data ); | |
else return $this->insertRow( $table, $column, $id, $data ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment