Return to Snippet

Revision: 11920
at February 23, 2009 20:48 by tclancy


Initial Code
<?php
Class DataConnection {
	
	var $oConn;
	var $oDB;
	var $oCurrentResource;
	var $aResultSet;
	var $bResultsFound;


	function DataConnection()
	{
		$this->oConn = mysql_connect(DB_SERVER, DB_USER, DB_USER_PASS) or die("No connection!");
		$this->oDB = mysql_select_db(DB_NAME) or die("No database!");
		register_shutdown_function(array(&$this, "closeConnection"));
		
		$this->bResultsFound = FALSE;
	}

	function query($sSQL, $bDebugMode = FALSE)
	{
		if ($bDebugMode) {
			echo $sSQL."<br />";
			exit;
		}
		$this->setCurrentResource(mysql_query($sSQL));
		if ($this->getNumberOfRows() == 0) {
			return false;
		}
		$this->setResultSet($this->getCurrentResource());
		return true;
	}

	function getNumberOfRows()
	{
		$iRows = 0;
		$oResource = $this->getCurrentResource();
		if (@mysql_num_rows($oResource) > 0) {
			$iRows = mysql_num_rows($oResource);
		}
		
		return $iRows;
	}

	function selectInfo($sSQL, $bDebugMode = FALSE)
	{
		$aReturn = FALSE;
		$this->query($sSQL, $bDebugMode);
		if ($this->getNumberOfRows() > 0) {
			$aReturn = $this->getResultSet();
		}
		return $aReturn;
	}
	
	function lastId()
	{
		return mysql_insert_id();
	}

	function closeConnection()
	{
		mysql_close($this->oConn);
	}

	function createSQLString($sInput)
	{
		$sInput = "'" . addslashes($sInput) . "'";
		return $sInput;
	}

	function logToDatabaseViaArray($aFieldInfo, $sTableName, $sSQLConstraints = "", $sMethod = "INSERT")
	{
		$sFields = "";
		$sValues = "";
		
		if ($sMethod == "UPDATE") {
			$sSQL = "UPDATE ".$sTableName." SET [** UPDATES **] ".$sSQLConstraints;
			$sUpdateString = "";
			foreach($aFieldInfo as $sKey => $sValue) {
				if (is_string($sValue)) {
					$sValue = $this->createSQLString($sValue);
				}
				
				$sUpdateString .= $sKey . " = ".$sValue.", ";
			}
			$sUpdateString = substr($sUpdateString, 0, strlen($sUpdateString)-2);
			$sSQL = str_replace("[** UPDATES **]", $sUpdateString,$sSQL);
			$this->query($sSQL);
			
		} else {
			$sSQL = "INSERT INTO ".$sTableName." ([** FIELDS **]) VALUES ([** VALUES **])";
			
			foreach($aFieldInfo as $sKey => $sValue) {
				if (is_string($sValue)) {
					$sValue = $this->createSQLString($sValue);
				}
				
				$sFields .= $sKey . ", ";
				$sValues .= $sValue . ", ";
			}
			$sFields = substr($sFields, 0, strlen($sFields)-2);
			$sValues = substr($sValues, 0, strlen($sValues)-2);
			
			$sSQL = str_replace("[** FIELDS **]", $sFields,$sSQL);

			$sSQL = str_replace("[** VALUES **]", $sValues,$sSQL);
			$this->query($sSQL);
		}
	}

	function fetchResultsByQuery($sSQL, $bDebugMode = FALSE)
	{
		$this->query($sSQL, $bDebugMode);
		return $this->getResultSet();
	}

	function getInsertId()
	{
		return mysql_insert_id($this->oConn);
	}

	function destroyCurrentResource()
	{
		$oResource =& $this->getCurrentResource();
		if (is_object($oResource)) {
			mysql_free_result($oResource);
		}
	}

	// MUTATORS & ACCESSORS
	function setCurrentResource($oNewResource)
	{
		$this->oCurrentResource = $oNewResource;
	}
	function getCurrentResource()
	{
		return $this->oCurrentResource;
	}

	function setResultSet($oNewRecordSet)
	{
		$iLoop = 0;
		$this->aResultSet = array();
		
		while($ROW = mysql_fetch_array($oNewRecordSet, MYSQL_ASSOC)) {
			while(list($key,$val) = each($ROW) ) {
				$this->aResultSet[$iLoop][$key] = $val;
          	}
			$iLoop++;
		}
		if (count($this->aResultSet) > 0) {
			$this->setResultFound(TRUE);
		} else {
			$this->setResultFound(FALSE);
		}
	}
	function getResultSet()
	{
		if (isset($this->aResultSet)) {
			return $this->aResultSet;
		} else {
			return FALSE;
		}
	}
	function getResultRow($iRow=0)
	{
		if (isset($this->aResultSet)) {
			return $this->aResultSet[$iRow];
		} else {
			return FALSE;
		}
	}

	function setResultFound($bNewStatus)
	{
		$this->bResultsFound = $bNewStatus;
	}
	function getResultFound()
	{
		return $this->bResultsFound;
	}

} // END CLASS
?>

Initial URL

                                

Initial Description

                                

Initial Title
PHP MySQL Data Connection Class

Initial Tags

                                

Initial Language
PHP