<?
/* Database handeling GEN 2
- now smarter result returning
Don't modify without written authorization
Created by: Randy Gerritse
Email: hide@address.com
©2002-2003, All rights reserved.
================================================================================ */
class Connection {
var $ident;
//main function, call on init
function Connection($dbhost="",$dbname="",$dbuser="",$dbpassword="") {
$GLOBALS["components"]["Connection"] = 1;
if ($dbhost=="") $dbhost = $GLOBALS["dbhost"];
if ($dbname=="") $dbname = $GLOBALS["dbname"];
if ($dbuser=="") $dbuser = $GLOBALS["dbuser"];
if ($dbpassword=="") $dbpassword = $GLOBALS["dbpassword"];
$this->ident = mysql_connect($dbhost,$dbuser,$dbpassword);
$tempint = mysql_select_db($dbname,$this->ident);
}
//execute a query
function execute($sql,$id="") {
$sql = ltrim($sql);
$resultset = mysql_query($sql,$this->ident);
if (strtolower(substr($sql,0,6)) == "select") {
$clients = null;
if ($resultset==0 || !$resultset)
return null;
else {
while ($resultset!=0 AND $result = mysql_fetch_array($resultset)) {
if ((isset($id) && is_numeric($id)) || (!isset($result["id"]) || empty($result["id"])))
$clients[] = $result;
elseif (isset($id) && !is_numeric($id))
$clients[$result[$id]] = $result;
else {
if (!isset($result["id"]))
return null;
else
$clients[$result["id"]] = $result;
}
}
return $clients;
}
} elseif (strtolower(substr($sql,0,6)) == "insert") {
return mysql_insert_id($this->ident);
} elseif (strtolower(substr($sql,0,6)) == "update" || strtolower(substr($sql,0,6)) == "delete") {
return mysql_affected_rows($this->ident);
}else
return null;
}
//switch database
function switchdb($newdbname) {
return mysql_select_db($newdbname,$this->ident);
}
//close the connection, always call this function at the bottom of your page that uses this class!
function close() {
$closeresult = mysql_close($this->ident);
}
}
?>