Scripts
Db MYSQL Connection class
Handige dbconnection class Vorige week geschreven voor een engels systeem, dus de compleet engels gecomment:)
db-mysql-connection-class
[code]
<?php
class cConnection {
#$sLink is the link returned by mysql_connect()
var $sLink;
#$rRes is the resultset returned by $this->dbQuery()
var $rRes;
#var $bLogQueries shows wether queries should be logged or not
var $bLogQueries;
#var $iQueries holds the number of queries on this page, if $bLogQueries = true
var $iQueries;
#var $aQueries holds all queries, if $bLogQueries = true
var $aQueries;
/*
constructor cConnection makes a mysql connection to the mysql server
set variables
*/
function cConnection($sDB,$sUser = "",$sPass = "",$sHost = "localhost",$bLogQueries = false){
$this->sLink = @mysql_connect($sHost,$sUser,$sPass);
if (!$this->sLink) {
echo mysql_error();
die;
}
if (!@mysql_select_db($sDB,$this->sLink)) {
echo mysql_error($this->sLink);
die;
}
$this->iQueries = 0;
$this->bLogQueries = $bLogQueries;
}
/*
dbQuery executes a query on the db selected by $this->cConnection,
if the querie fails the mysql_error is shown and script execution
is aborted. If $this->bLogQueris = true the query counter is increased
and the query is added to the query array.
dbQuery($sQuery) returns a mysql resultset if succesful and die's with a
msyql error if it fails
*/
function dbQuery($sQuery) {
#make connection if it's the first query
if (!isset($this->sLink) || $this->sLink == "") $this->cConnect($this->sDB,$this->sUser,$this->sPass,$this->sHost,$this->bLog);
$rResult = mysql_query($sQuery,$this->sLink);
if(!$rResult) {
echo mysql_error($this->sLink);
die;
}
if ($this->bLogQueries) {
$this->aQueries[] = $sQuery;
$this->iQueries++;
}
$this->rRes = $rResult;
return $rResult;
}
/*
$rResult is the resultset retrieved by a call to $this->dbQuery or
empty if you want to use the last dbQuery or a query string if you
want to execute that query first. Returns a array fetched by $iType, and pushes it all on 1
stack
*/
function dbFetchSmarty ( $rResult, $iType ) {
if( !is_resource($rResult) )
if(substr(strtoupper($rResult),0,6) == "SELECT")
$rResult = $this->dbQuery( $rResult );
if ( !$rResult ) {
echo mysql_error($this->sLink);
die;
} else {
$aReturn = array();
while ( $aFetched = mysql_fetch_array( $rResult , $iType ) ) array_push( $aReturn, $aFetched );
return $aReturn;
}
}
/*
*/
function dbFetchResult ( $rResult, $iRow = 0, $mField = 0 ) {
if( !is_resource($rResult) )
if(substr(strtoupper($rResult),0,6) == "SELECT")
$rResult = $this->dbQuery( $rResult );
if ( !$rResult ) {
echo mysql_error($this->sLink);
die;
} else {
return mysql_result ( $rResult, $iRow, $mField );
}
}
/*
$rResult is the resultset retrieved by a call to $this->dbQuery or
empty if you want to use the last dbQuery or a query string if you
want to execute that query first. Returns a mysql error if the query
fails, returns an integer mysql_num_rows() otherwise
*/
function dbRows($rResult = false){
if(!is_resource($rResult)){
if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
else return mysql_num_rows($this->rRes);
}
if(!$rResult) {
echo mysql_error($this->sLink);
die;
} else return mysql_num_rows($rResult);
}
/*
accepts a sql query or resultset and returns fetched row
*/
function dbFetchRow ($rResult = false) {
if(!is_resource($rResult)){
if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
else return mysql_fetch_row($this->rRes);
}
if(!$rResult) {
echo mysql_error($this->sLink);
die;
} else return mysql_fetch_row($rResult);
}
function dbFetchArray ( $rResult = false, $iType ) {
if(!is_resource($rResult)){
if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
else return mysql_fetch_array($this->rRes, $iType);
}
if(!$rResult) {
echo mysql_error($this->sLink);
die;
} else return mysql_fetch_array($rResult, $iType);
}
/*
returns the mysql_affected_rows of the last query
*/
function dbAffectedRows() {
return mysql_affected_rows($this->sLink);
}
/*
$rResult is a resultset retrieved by $this->dbQuery or false if you want
to use the last dbQuery or a query string if you want to execute that query
first. Returns a mysql_error if the query fails, returns a fetched object
otherwise.
*/
function dbFetchObject($rResult = false){
if (!is_resource($rResult)){
if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
else $rResult = $this->rRes;
}
if(!$rResult) {
echo mysql_error($this->sLink);
die;
} else return mysql_fetch_object($rResult);
}
/*
if $this->bLogQueries = true, dbOutputQueries() outputs all used queries
on this page, returns nothing otherwise.
*/
function dbOutputQueries() {
if ($this->bLogQueries) foreach ($this->aQueries as $sValue) echo $sValue.'<br />';
}
/*
returns the auto_increment column value for your last insert query,
returns 0 if no auto_increment column was found or the query was no
insert query
*/
function dbInsertID() {
return mysql_insert_id($this->sLink);
}
/*
closes the connection to the mysql server
*/
function dbClose(){
mysql_close($this->sLink);
}
/*
frees $rResult's memory
*/
function dbFreeResult($rResult){
mysql_free_result($rResult);
}
}
$oCon = new cConnection("db","user","pass","localhost", false);
[/code]
Reacties
0