class sql {
private $query;
private $return;
private $numrows;
private $result;
private $db;
private $type;
private $dbtypes = array('mysql', 'pgsql', 'mssql');
public function __construct ( $dbhost, $dbuser, $dbpass, $dbname, $dbtype = 'mysql' ) {
$this->query = '';
$this->return = '';
$this->numrows = 0;
$this->result = '';
if(!in_array($dbtype, $this->dbtypes)) {
exit('Sorry, this database type is not available.');
}
switch ($dbtype) {
case 'mysql':
$this->type = 'mysql';
$constr = 'mysql:host='.$dbhost.';dbname='.$dbname;
break;
case 'mssql':
$this->type = 'sqlsrv';
$constr = 'sqlsrv:Server='.$dbhost.';Database='.$dbname;
break;
case 'pgsql':
$this->type = 'pgsql';
$constr = 'pgsql:dbname='.$dbname.';host='.$dbhost;
break;
default:
$this->type = 'mysql';
$constr = 'mysql:host='.$dbhost.';dbname='.$dbname;
break;
}
try {
$this->db = new PDO($constr, $dbuser, $dbpass);
} catch (PDOException $e) {
echo 'Cannot connect to database.';
}
}
function close () {
$this->db = null;
}
// === Function to log SQL problems ===
function logger ( $qry, $err = '' ) {
$qry = addslashes($qry);
$err = addslashes($err);
if(function_exists(makeLog)) makeLog("Error in MySQL query. The Query was: \"$qry\". The error gave was: $err");
}
function error ( $qry, $erOb, $ret ) {
$error = $erOb->errorInfo();
$this->logger($qry, $error[2]);
if($ret == 1) {
die('Error in SQL: '.$error[2].' - Query: '.$qry);
} else if ($ret == 2) {
echo 'Transaction Error';
}
}
function update ( $qry, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry);
$return = $query->execute();
if(!$return) {
$this->error($qry, $query, $ret);
} else {
if($ret == 3) {
echo 'Successful Transaction..
';
}
}
}
function updateA ( $table, $items, $where = '' ) {
global $dbg, $debug;
if(is_array($items)) {
foreach($items as $key => $value) {
if(strtoupper($value) == 'NULL') $ups[] = $key." = ".$value;
else $ups[] = $key." = :".$key;
$farray[':'.$key] = $value;
}
$ups = implode(', ', $ups);
$qry = "UPDATE $table SET $ups $where";
$query = $this->db->prepare($qry);
$return = $query->execute($farray);
} else {
echo 'Nothing to do. Please include data fields to update.';
}
}
function insert ( $qry, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry);
$return = $query->execute();
if(!$return) {
$this->error($qry, $query, $ret);
} else {
if($ret == 3) {
echo 'Successful Transaction..
';
}
}
}
function insertA ( $table, $items ) {
global $dbg, $debug;
if(is_array($items)) {
foreach($items as $key => $value) {
$rows[] = $key;
if(strtoupper($value) == 'NULL') $vals[] = $value;
else $vals[] = ":".$key;
$farray[':'.$key] = $value;
}
$rows = implode(', ', $rows);
$vals = implode(', ', $vals);
$qry = "INSERT INTO $table ($rows) VALUES ($vals)";
$dbg[] = $qry;
$query = $this->db->prepare($qry);
$return = $query->execute($farray);
} else {
echo 'Nothing to do. Please include data fields to insert.';
}
}
function remove ( $qry, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry);
$return = $query->execute();
if(!$return) {
$this->error($qry, $query, $ret);
} else {
if($ret == 3) {
echo 'Successful Transaction..
';
}
}
}
function singleSel ( $qry, $type = 1, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$final = $query->execute();
$this->setRows($query);
if($this->numrows > 0 || $this->type == 'pgsql') {
$return = $query->fetchColumn(0);
} else {
$return = '';
}
if(!$return && $return < '') {
$this->error($qry, $query, $ret);
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo $return;
}
}
}
function rowSelect ( $qry, $type = 1, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$final = $query->execute();
$this->setRows($query);
if($this->numrows > 0 || $this->type == 'pgsql') {
$return = $query->fetch(PDO::FETCH_BOTH);
} else {
$return = '';
}
if(!$return && $return < '') {
$this->error($qry, $query, $ret);
} else {
if($type == 1) {
return $return;
} else if($type == 2) {
echo implode(';', $return);
}
}
}
function rowSelectRet ( $qry, $ret = 1 ) {
global $dbg, $debug;
$dbg[] = $qry;
$query = $this->db->prepare($qry, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$final = $query->execute();
$this->setRows($query);
if(!$final) {
$this->error($qry, $query, $ret);
} else {
return $query->fetchAll();
}
}
function setRows ( $res ) {
$this->numrows = $res->rowCount();
}
function getRows () {
return $this->numrows;
}
function getLastId () {
if($this->type == 'pgsql') {
return $this->singleSel("SELECT lastval();");
} else return $this->db->lastInsertId();
}
function sanSql ( $string ) {
// remove whitespaces (not a must though)
$string = trim($string);
// apply stripslashes if magic_quotes_gpc is enabled
if(get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
$string = $this->db->quote($string);
$string = substr($string, 1, -1);
return $string;
}
}
?>