Location: PHPKode > scripts > DBAccess > dbaccess/dbaccess.php
<?
/*******************************************************************
<( DBAccess v1.1 )>

Author: Thiago Canozzo Lahr - Campinas - SP - Brazil
E-mail: hide@address.com

Suported Databases: InterBase, MySQL and PostgreSQL

--------------------------------------------------------------------
Change Log:
v1.1 (2005/04/19)
- Added DEBUG_MODE in order to prevent users to see sql error messages

v1.0.1 (2002/07/14)
- Missing some brace in line 57, 179 and one semicolon in line 170
--------------------------------------------------------------------

Example:

Interbase:
$variable = new DBAccess('9.179.60.43','SYSDBA','masterkey','/dir/databasename.gdb');

#SELECT:
$variable->setSQL("select * from table");
$variable->Open();

#if you want print the row 10:
echo $variable->Result[field_name][9]; //prints row 10

#if you want print all rows:
for($i=0;$i<$variable->RecordCount;$i++) {
	echo $variable->Result[field_name][$i];
	}


#INSERT, DELETE, UPDATE
$variable->StartTransaction();
$variable->setSQL("delete * from table");
$variable->ExecSQL();
$variable->Commit; // or $variable->Rollback();

$variable->Disconnect();


*******************************************************************/

//DEFINE("DATABASE_TYPE","0");		// Interbase
DEFINE("DATABASE_TYPE","1");	// MySQL
//DEFINE("DATABASE_TYPE","2");		// PostgreSQL

DEFINE("DEBUG_MODE","0");		// DEBUG MODE
								// 0 = off
								// 1 = on

class DBAccess {
        var $Result = array();
        var $RecordCount;
        var $SQL;
        var $DBConn;
        var $aux = array();

        function DBAccess($dbhost,$dbuser,$dbpass,$dbname) {
				switch(DATABASE_TYPE) {
					case 0: $this->DBConn = ibase_connect($dbhost.':'.$dbname,$dbuser,$dbpass) or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); break;
					case 1: $this->DBConn = mysql_connect($dbhost,$dbuser,$dbpass) or die("Error: ".$this->DebugMode(mysql_error())."<br>"); 
							mysql_select_db($dbname) or die("Erro: ".mysql_error()."<br>"); break;
					case 2: $this->DBConn = pg_connect("host=".$dbhost." dbname=".$dbname." user=".$dbuser." password=".$dbpass."") or die("Error: ".$this->DebugMode(pg_last_error($this->DBConn))."<br>"); break;
					default: break;
				}
		}

        function setSQL($strSQL) {
                $this->SQL = $strSQL;
        }

        function Open() {
                switch(DATABASE_TYPE) {
					case 0: $query = ibase_query($this->DBConn,$this->SQL) or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); 
							$num_fields = ibase_num_fields($query); break;
					case 1: $query = mysql_query($this->SQL,$this->DBConn) or die("Error: ".$this->DebugMode(mysql_error())."<br>");
							$num_fields = mysql_num_fields($query); break;
					case 2: $query = pg_query($this->DBConn,$this->SQL) or die("Error: ".$this->DebugMode(pg_last_error($this->DBConn))."<br>"); 
							$num_fields = pg_num_fields($query); break;
					default: $num_fields = 0; break;
				}
                $jj = 0;
                $aux_count=1;

                switch(DATABASE_TYPE) {
					case 0: 
							while($row = ibase_fetch_row($query)) {
                        		for($ii=0;$ii<$num_fields;$ii++) {
                                	$field_info = ibase_field_info($query,$ii);
                                	$field_name = strtolower($field_info['name']);
                                	for($aa=0;$aa<$ii;$aa++) {
                                    	if($aux[$aa] == $field_name) {
                                        	$field_name = $field_name.$aux_count;
                                        	$aux_count++;
                                    		}
									}
                                	$aux[$ii] = $field_name;
                                	$this->Result[$field_name][$jj] = $row[$ii];
                                }
                        		$jj = $jj + 1;
                        		$aux_count=1;
                        	}
                			$this->RecordCount = $jj;
                			ibase_free_result($query) or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>");
							break;
							
					case 1: 
							while($row = mysql_fetch_row($query)) {
                        		for($ii=0;$ii<$num_fields;$ii++) {
                                	$field_name = mysql_field_name($query,$ii);
                                	for($aa=0;$aa<$ii;$aa++) {
                                    	if($aux[$aa] == $field_name) {
                                        	$field_name = $field_name.$aux_count;
                                        	$aux_count++;
                                    		}
									}
                                	$aux[$ii] = $field_name;
                                	$this->Result[$field_name][$jj] = $row[$ii];
                                }
                        		$jj = $jj + 1;
                        		$aux_count=1;
                        	}
                			$this->RecordCount = $jj;
                			mysql_free_result($query) or die("Error: ".$this->DebugMode(mysql_error())."<br>");
							break;
							
					case 2: 
							while($row = pg_fetch_row($query)) {
                        		for($ii=0;$ii<$num_fields;$ii++) {
                                	$field_name = pg_field_name($query,$ii);
                                	for($aa=0;$aa<$ii;$aa++) {
                                    	if($aux[$aa] == $field_name) {
                                        	$field_name = $field_name.$aux_count;
                                        	$aux_count++;
                                    		}
									}
                                	$aux[$ii] = $field_name;
                                	$this->Result[$field_name][$jj] = $row[$ii];
                                }
                        		$jj = $jj + 1;
                        		$aux_count=1;
                        	}
                			$this->RecordCount = $jj;
                			pg_free_result($query) or die("Error: ".$this->DebugMode(pg_last_error($this->DBConn))."<br>");
							break;
				
					default: break;
				}
        }

        function ExecSQL() {
				switch(DATABASE_TYPE) {
					case 0: $sql = ibase_prepare($this->SQL) or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>");
							ibase_execute($sql); break;
							
					case 1: $sql = mysql_query($this->SQL,$this->DBConn) or die("Error: ".$this->DebugMode(mysql_error())."<br>"); break;

					case 2: $sql = pg_query($this->DBConn,$this->SQL) or die("Error: ".$this->DebugMode(pg_last_error($this->DBConn))."<br>"); break;
				}
		}
		
	function Disconnect() {
			switch(DATABASE_TYPE) {
				case 0: ibase_close($this->DBConn) or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); break;
				case 1: mysql_close($this->DBConn) or die("Error: ".$this->DebugMode(mysql_error())."<br>"); break;
				case 2: pg_close($this->DBConn) or die("Error: ".$this->DebugMode(pg_last_error($this->DBConn))."<br>"); break;
			}
		}
		
	function StartTransaction() {
			switch(DATABASE_TYPE) {
				case 0: ibase_trans() or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); break;
			}
		}
		
	function Commit() {
			switch(DATABASE_TYPE) {
				case 0: ibase_commit() or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); break;
			}
		}
		
	function Rollback() {
			switch(DATABASE_TYPE) {
				case 0: ibase_rollback() or die("Error: ".$this->DebugMode(ibase_errmsg())."<br>"); break;
			}
		}
	
	function DebugMode($error_msg) {
		if(DEBUG_MODE) {
			return $error_msg;
		}
		else {
			return "Debug mode off";
		}
	}
		
	}

?>
Return current item: DBAccess