Skip to content

Instantly share code, notes, and snippets.

@mikevalstar
Created January 31, 2012 13:43
Show Gist options
  • Save mikevalstar/1710558 to your computer and use it in GitHub Desktop.
Save mikevalstar/1710558 to your computer and use it in GitHub Desktop.
<?php
class gPDO{
public static function init(){
$GLOBALS['gPDO'] = array();
$GLOBALS['gPDO']['server'] = '';
$GLOBALS['gPDO']['user'] = '';
$GLOBALS['gPDO']['password'] = '';
$GLOBALS['gPDO']['db'] = '';
$GLOBALS['gPDO']['charset'] = 'UTF-8';
}
public static function set($var, $val){
switch($var){
case 'server':
case 'user':
case 'password':
case 'db':
case 'charset':
$GLOBALS['gPDO'][$var] = $val;
break;
default:
trigger_error('Attempting to set ' . $var . ' is not possible.');
break;
}
}
public static function prepare($query, $options = array(), $supress_error = false){
gPDO::_connect();
if ( $statement = $GLOBALS['gPDO']['con']->prepare( $query, $options ) ){
return $statement;
}elseif(!$supress_error){
trigger_error($query . ' Produced an error: <b>' . print_r($GLOBALS['gPDO']['con']->errorInfo(), true) . '</b>');
//trigger_error('Error Preparing Statement', $query, $options, print_r($GLOBALS['gPDO']['con']->errorInfo(), true));
return false;
}else{
return false;
}
}
public static function execute($query, $aParams = array(), $supress_error = false) {
gPDO::_connect();
$results = $query->execute($aParams);
if($results){
return $results;
}elseif(!$supress_error){
echo "error" .print_r($query->errorInfo(), true);
trigger_error(' Produced an error: <b>' . print_r($query->errorInfo(), true) . '</b>');
//trigger_error('Error Executing Query', $this->queryString, $aParams, print_r($this->errorInfo(), true));
return false;
}else{
return false;
}
return $results;
}
public static function prepare_execute($query, $parms = array(), $options = array(), $supress_error = false){
if(count($parms) == 0){
$q = gPDO::query($query, $supress_error);
return $q;
}elseif($q = gPDO::prepare($query, $options, $supress_error)){
gPDO::execute($q, $parms, $supress_error);
return $q;
}
return false;
}
public static function query($query, $supress_error = false){
gPDO::_connect();
if ( $statement = $GLOBALS['gPDO']['con']->query( $query ) ){
return $statement;
}elseif(!$supress_error){
trigger_error($query . ' Produced an error: <b>' . print_r($GLOBALS['gPDO']['con']->errorInfo(), true) . '</b>');
//trigger_error('Error Preparing Statement', $query, array(), print_r($this->errorInfo(), true));
return false;
}else{
return false;
}
}
public static function numrows(){
$sql = 'SELECT FOUND_ROWS() AS cnt';
$query = gPDO::prepare_execute($sql);
$row = $query->fetch();
return $row['cnt'];
}
public static function lastInsertId(){
return $GLOBALS['gPDO']['con']->lastInsertId();
}
public static function _connect(){
if(!isset($GLOBALS['gPDO']['con'])){
$GLOBALS['gPDO']['con'] =
new PDO("mysql:host={$GLOBALS['gPDO']['server']};dbname={$GLOBALS['gPDO']['db']};charset={$GLOBALS['gPDO']['charset']}",
$GLOBALS['gPDO']['user'],
$GLOBALS['gPDO']['password'],
array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => true)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment