IsConnected()) $dba->SelectScheme($dbName); // this is an example of the script code $req = "SELECT field1, field2, field3 FROM table1, teble2 "; $res = $dba->GetArray($req); $nb = $dba->GetRowsNb(); $firs_row_firs_field = $res[0][0]; $first_row_second_field = $res[0][1]; ... */ class DbAccess { var $mDebug; // boolean var $mIsPers; // boolean (persistent connection var $mQuery; // last quesry var $mResult; // actual result resource var $mLink; // connection handle var $mDbHost; var $mDbName; // database name var $mDbUser; var $mDbPasswd; var $mErrorCode; // last error code var $mErrorText; // last error description var $mRowsNb; // nb of records from tha last query var $mLogFilename; // error log filename var $mIsConnected; // boolean var $schema; function DbAccess($dbHost, $dbUser, $dbPasswd, $isPers = false) { $this->mDbHost = $dbHost; $this->mDbUser = $dbUser; $this->mDbPasswd = $dbPasswd; $this->mDebug = false; $this->mIsPers = $isPers; if ($this->Connect()) { $this->mIsConnected = true; } else { $this->mIsConnected = false; } } function Connect() { if ($this->mIsPers) { $connect_func = "mysql_pconnect"; } else { $connect_func = "mysql_connect"; } @$this->mLink = $connect_func($this->mDbHost, $this->mDbUser, $this->mDbPasswd); if (!$this->mLink) { $this->HandleError(mysql_errno(), mysql_error()); return false; } return true; } function IsConnected() { return $this->mIsConnected; } function SelectScheme($dbScheme) { if (!$this->IsConnected()) { $this->HandleError(-1, "Not connected to MySQL"); return false; } if (!mysql_select_db($dbScheme, $this->mLink)) { $this->HandleError(mysql_errno($this->mLink), mysql_error($this->mLink)); return false; } $this->schema = $dbScheme; return true; } function Close() { if (!$this->mLink) return false; if(!mysql_close($this->mLink)) { $this->HandleError(mysql_errno($this->mLink), mysql_error($this->mLink)); return false; } $this->mIsConnected = false; return true; } function HandleError($errorNb, $errorText) { $this->mErrorCode = $errorNb; $this->mErrorText = $errorText; $error = date("Y-M-d H:i:s")." ".$errorNb." : ".$errorText."\n"; if ($this->mDebug) { echo "
".$error;
                        echo $this->mQuery;
		}
		if ($this->mLogFilename)
		{
			if (@$file = fopen($this->mLogFilename, "a"))
			{
				@fwrite($file, $error);
				@fclose($file);
			}
		}
	}

	function GetLastErrorCode()
	{
		return $this->mErrorCode;
	}

	function GetLastErrorText()
	{
		return $this->mErrorText;
	}

	function ClearError()
	{
		$this->mErrorText = false;
		$this->mErrorCode = false;
	}

	function SetDebug($dbDebug)
	{
		if (is_bool($dbDebug)) {
			$this->mDebug = $dbDebug;
		}
	}

	function SetLogFile($dbLog)
	{
		$this->mLogFilename = $dbLog;
	}

	function MakeQuery($query)
	{
		$this->SelectScheme($this->schema);
		if (!$this->mLink)
		{
			$this->HandleError(-1, "Not connected to MySQL");
			return false;
		}
		$this->mQuery = $query;
		$res = mysql_query($this->mQuery, $this->mLink);
		if (mysql_errno($this->mLink))
		{
			$this->HandleError(mysql_errno($this->mLink), mysql_error($this->mLink));
			return false;
		}
		$this->mResult = $res;
		return true;
	}

	function GetRowsNb()
	{
		return $this->mRowsNb;
	}

	function GetArray($query)
	{
		if ($this->MakeQuery($query))
		{
            while ($row = mysql_fetch_row($this->mResult)) {
                $ret[] = $row;
            }
			$this->mRowsNb = mysql_num_rows($this->mResult);
			mysql_free_result($this->mResult);
			$this->ClearError();
			return $ret;
		}
		return false;
	}
	
	function GetParamArray($query) {
		$res = $this->GetArray($query);
		$final = array();
		foreach ($res as $t) $final[$t[0]] = $t[1];
		return $final;
	}

	function GetRow($query)
	{
		if ($this->MakeQuery($query))
		{
			$ret = mysql_fetch_row($this->mResult);
			if ($ret)
			{
			 $this->mRowsNb = 1;
			}
			else
			{
			    $this->mRowsNb = 0;
			}
			mysql_free_result($this->mResult);
			$this->ClearError();
			return $ret;
		}
		return false;
	}

	function GetColumn($query)
	{
		if ($temp_res = $this->GetArray($query))
		{
			foreach ($temp_res as $row)
				$ret[] = $row[0];
			$this->ClearError();
			return $ret;
		}
		return false;
	}

	function GetCount($query)
	{
		if ($this->MakeQuery($query))
		{
			$this->mRowsNb = mysql_num_rows($this->mResult);
			mysql_free_result($this->mResult);
			$this->ClearError();
			return $this->mRowsNb;
		}
		return false;
	}

	function GetOne($query)
	{
		if ($this->MakeQuery($query))
		{
			$res = mysql_fetch_row($this->mResult);
			$ret = $res[0];
			if ($ret)
			{
			 $this->mRowsNb = 1;
			}
			else
			{
			    $this->mRowsNb = 0;
			}
			mysql_free_result($this->mResult);
			$this->ClearError();
			return $ret;
		}
		return false;
	}

	function Insert($query)
	{
		if ($this->MakeQuery($query))
		{
			$this->mRowsNb = mysql_affected_rows($this->mLink);
			$this->ClearError();
			return true;
		}
		return false;
	}

	function InsertReturning($query, &$rId)
	{
		if ($this->MakeQuery($query))
		{
			$this->mRowsNb = mysql_affected_rows($this->mLink);
			$rId = mysql_insert_id($this->mLink);
			$this->ClearError();
			return true;
		}
		return false;
	}

	function Delete($query)
	{
		if ($this->MakeQuery($query))
		{
			$this->mRowsNb = mysql_affected_rows($this->mLink);
			$this->ClearError();
			return true;
		}
		return false;
	}

	function Update($query) {
		if ($this->MakeQuery($query))
		{
			$this->mRowsNb = mysql_affected_rows($this->mLink);
			$this->ClearError();
			return true;
		}
		return false;
	}

    function GetDate($mysql_datetime)
    {
        return(split ("-", substr($mysql_datetime,0, 10)));
    }

    function GetTime($mysql_datetime) {
        return(split (":", substr($mysql_datetime, 11)));
    }

    function GetDateTime($mysql_datetime) {
        $res = $this->GetDate($mysql_datetime);
        list($a,$b,$c) = $this->GetTime($mysql_datetime);
        $res[3] = $a;
        $res[4] = $b;
        $res[5] = $c;
        return($res);
    }
    
    function GetTitledRow($query) {
    	
    	if ($this->MakeQuery($query)) {
    		
    		$row = mysql_fetch_row($this->mResult);
    		$finalRow = array();
    		if (!is_array($row) || count($row) == 0) return false;
    		foreach ($row as $key => $value) {
    			$finalRow[mysql_field_name($this->mResult, $key)] = $value;
    		}
    		$this->ClearError();
    		return $finalRow;
    	}
    }
    
    function GetTableData($tableName) {
    	
    	$final = array();
    	
    	$req = "SHOW COLUMNS FROM " . $tableName;
    	$res = $this->GetArray($req);
    	
    	if ($this->GetRowsNb() > 0) {
	    	foreach ($res as $t) {
	    		$final[$t[0]] = $t[1];
	    	}
    	} else {
    		return false;
    	}
    	
    	return $final;
    }
}
?>