Skip to content

Instantly share code, notes, and snippets.

@alexburner
Created February 9, 2012 23:02
Show Gist options
  • Save alexburner/1784086 to your computer and use it in GitHub Desktop.
Save alexburner/1784086 to your computer and use it in GitHub Desktop.
building a MySQL wrapper
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