All pastes #952297 Raw Edit

Mine

public text v1 · immutable
#952297 ·published 2008-03-22 01:09 UTC
rendered paste body
<?php
/*
------------------------------------------
Frans Reesink
18-04-2007
------------------------------------------
*/


Class db {
    
    var $m_aSettings;
    
    // construct: load the settings
    function db() {
        $this->load_ini();
    }
    
	// Function load_ini
	// Parameters: none
	// Returns: An array with all the settings
	function load_ini() {
        if (empty($this->m_aSettings)) {
		    $this->m_aSettings = parse_ini_file("inc/settings.ini", true);
        }
	}
	              
    // make a connection with the database
	function db_connect() {
		$this->link = mysql_connect($this->m_aSettings['db']['host'], $this->m_aSettings['db']['username'], $this->m_aSettings['db']['password']);
		mysql_select_db($this->m_aSettings['db']['database']);
	}
	
	function db_close() {
		mysql_close($this->link);
	}
	
	// Security (sql injectie, cross site scripting)
	// Parameters: $p_var can be a varchar but also an array
	// Returns: It spits out the same var or array but with addslashes and htmlspecialchars
	function sanitize($p_var) {
		if (is_array($p_var)) {
			$l_aArray = array();
				foreach ($p_var as $l_sField=>$l_sValue) {
					$l_sValue = addslashes(htmlspecialchars($l_sValue));
					$l_aArray[$l_sField] = $l_sValue;
				}
			return $l_aArray;
		} else {
			$p_var = addslashes(htmlspecialchars($p_var));
			return $p_var;
		}
	}
	
	// Function to check required fiels
	// Parameters: Array with all the fields and values ($bla=>"meep");
	// Returns: True or false
	function check_reqfields($p_aArray) {
		$l_b = true;
		foreach ($p_aArray as $l_sField=>$l_aValue) {
			if (empty($l_aValue)) {
			$l_b = false;
			}
		}
		return $l_b;
	}
	
	
	// Insert query function
	// Parameters: Table name (string) and an array with values like fieldname=>value
	// Returns: True or false
	function insert($p_sTableName, $p_aInsertvalues) {
		
		$p_aInsertValues = $this->sanitize($p_aInsertValues);
		
		$this->db_connect();
		
		foreach($p_aInsertvalues as $l_sFieldname=>$l_sFieldvalue) {
			 $l_sSql .= $l_sFieldname."='".$l_sFieldvalue."', "; 
		}
		
		$l_sSql = substr($l_sSql, 0, -2);
		 
		return mysql_query("INSERT INTO " . $p_sTableName . " SET " . $l_sSql);
		
		$this->db_close();
	}
	
	// Delete query function
	// Parameters: Tablename (string), fieldname (string) and a value for the field to identify the row that should be deleted
	// Returns: True or false
	function delete($p_sTableName, $p_sField, $p_sValue) {
		$p_sValue = $this->sanitize($p_sValue);
		$this->db_connect();
		return mysql_query("DELETE FROM " . $p_sTableName . " WHERE " .  $p_sField . " = " . $p_sValue); 
		$this->db_close();
	}
	
	
	// Update query function
	// Parameters: Tablename (string), Field=>Value(array) and the ID of the row that should be updated
	// Returns: True of false
	function update($p_sTableName, $p_aUpdatevalues, $p_iId) {
	
		$p_aUpdatevalues = $this->sanitize($p_aUpdatevalues);
		
		$this->db_connect();

		foreach($p_aUpdatevalues as $l_sFieldname=>$l_sFieldvalue) {
			$l_sSql .= $l_sFieldname."='".$l_sFieldvalue."', ";
		}
		
		$l_sSql = substr($l_sSql, 0, -2);
			
		return mysql_query("UPDATE " . $p_sTableName . " SET " . $l_sSql . " WHERE userID = " . $p_iId); 
		
		$this->db_close();
	}
	
	
	// General query function
	// Parameters: The query you would like to run
	// Returns: An array with all the results
	function do_query($p_sQuery) {
		$this->db_connect();
		$l_oResults = mysql_query($p_sQuery);
			while($l_aItems = mysql_fetch_assoc($l_oResults)) {
			$l_aResults[] = $l_aItems;
			}
		return $l_aResults;
		$this->db_close();
	}
	
	
	// Function to save enum values
	// Parameters: Tablename and a fieldname (enum);
	// Returns: An array with all the enum values
	function get_enum_values($p_sTable, $p_sField) {
		$this->db_connect();
		$l_aAr = $this->do_query("DESCRIBE " . $p_sTable . " " . $p_sField);
		$l_sEnumstring = $l_aAr[0]['Type'];
		$l_sEnums = eregi_replace('(enum\(|\)|\')', '', $l_sEnumstring);
		$l_aEnums = explode(',', $l_sEnums);
		return $l_aEnums;
		$this->db_close();
	}
	
}