/**
* @function insQuery
* Used for INSERT query,
* returns false if failed, and returns insert id on success
*/
function insQuery($sql) {
$this->resetError();
if($this->type=='mysql') {
$q = @mysql_query($sql,$this->conn);
if($q == false) {
$this->errno = mysql_errno($this->conn);
$this->error = mysql_error($this->conn);
return false;
} else {
return mysql_insert_id($this->conn);
}
}
}
/**
* @function selQuery
* Used for SELECT query of multiple records
* returns resource to use, or false if failed.
*/
function selQuery($sql) {
$this->resetError();
if($this->type=='mysql') {
$q = @mysql_query($sql,$this->conn);
if($q == false) {
$this->errno = mysql_errno($this->conn);
$this->error = mysql_error($this->conn);
return false;
} else {
return $q;
}
}
}
/**
* @function fieldQuery
* Used for SELECT query which involves a single field, in a single row
* returns the first field, in the first row (used for things like COUNT(*))
*/
function fieldQuery($sql) {
$this->resetError();
if($this->type=='mysql') {
$q = @mysql_query($sql,$this->conn);
if ($q == false) {
$this->errno = mysql_errno($this->conn);
$this->error = mysql_error($this->conn);
return false;
} else {
return mysql_result($q,0,0);
}
}
}
/**
* @function affectQuery
* Used for UPDATE/REPLACE/DELETE on the database
* returns false if there was an error, or returns the number of affected rows if success
*/
function affectQuery($sql) {
$this->resetError();
if($this->type=='mysql') {
$q = @mysql_query($sql,$this->conn);
if ($q == false) {
$this->errno = mysql_errno($this->conn);
$this->error = mysql_error($this->conn);
} else {
return mysql_affected_rows($this->conn);
}
}
}
/**
* @function fetch
* Used to fetch an associative array of a row
* returns array, or false if there is no more rows
*/
function fetch($resource) {
if($this->type=='mysql') {
return mysql_fetch_assoc($resource);
}
}
/**
* @function numrows
* Used to get the number of rows from a query
* returns the numbers from a query or false on failure
*/
function numrows($resource) {
if($this->type=='mysql') {
return mysql_num_rows($resource);
}
}
/**
* @function free
* free the result resource
*/
function free($resource) {
if($this->type=='mysql') {
return @mysql_free_result($resource);
}
}
/**
* @function errno
* returns the error number of the last error
*/
function errno() {
return $this->errno;
}
/**
* @functoin error
* returns the error description of the last error
*/
function error() {
return $this->error;
}
/**
* @function resetError
* resets the error values, for a new query
*/
function resetError() {
$this->errno = false;
$this->error = false;
}
}
?>
Usage Example
<?php
$db = new DB($config['sql_type'], $config['sql_host'], $config['sql_user'], $config['sql_pass'], $config['sql_dbase']);
if($db->error()) {
die('Failed to connect to database: '.$db->error());
}
$q = $db->selQuery('SELECT field1,field2 FROM table WHERE id='.intval($id));
if($q == false) {
die($db->error());
}
if($db->numRows($q) == 0) {
die('No data');
}
var_dump($db->fetch($q));
?>