<?php

	/*
		-------------------------------------------------------------------------
		PhpMySql Copyright © 2003 GRAVICAL - gravical@hotmail.com
		November 18, 2003
		-------------------------------------------------------------------------
	*/

	// Yes we will by default display errors
	$displayMySqlErrors = 1;
	
	// ----------------------------------------------
	// Sql Row
	
	class PhpSqlRow {

		var $m_sData;

		function PhpSqlRow()
		{
			$this->m_sData = array();
		}
		
		function GetValue($sName)
		{
			$sName = strtolower($sName);
			if(!isset($this->m_sData[ $sName  ]))
				return "";
			else
				return $this->m_sData[ $sName ];
		}

		function IsValuePresent($sName)
		{
			$sName = strtolower($sName);
			return isset($this->m_sData[ $sName  ]);
		}

		function SetAsResult($result)
		{
			$this->m_sData = mysql_fetch_array($result);
		}

		function SetAsArray($a)
		{
			$this->m_sData = $a;
			return $this->m_sData;
		}

		function SetColumn($sName, $sValue)
		{
			$sName = strtolower($sName);
			$this->m_sData[ $sName ] = $sValue;
		}

		function SetNameValue($sName, $sValue)
		{
			$sName = strtolower($sName);
			$this->m_sData[ $sName ] = $sValue;
		}
	};

	// ----------------------------------------------
	// Helpers

	function SelectSingleRow($query)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;

		// Create the row
		$sqlrow = new PhpSqlRow;
		
		// Begin a sql connection
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);

		// Execute the query		
		$result = mysql_query($query, $db);

		// Check for errors
		DisplaySqlError($db, $query);

		// If we loaded the result
		if($result != null)
		{
			// Transfer to the row
			$sqlrow->SetAsResult($result);

			// Cleanup
			mysql_free_result($result);
		}
		
		// Close
		mysql_close($db);
		
		// Return it
		return $sqlrow;		
	}

	function SelectScalar($query)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;

		// Create the row
		$sqlrow = new PhpSqlRow;
		$scalar = "";
		
		// Begin a sql connection
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);

		// Execute the query		
		$result = mysql_query($query, $db);

		// Check for errors
		DisplaySqlError($db, $query);

		// If we loaded the result
		if($result != null)
		{
			// Transfer to the row
			$a = mysql_fetch_array($result);

			// Set the value
			$scalar = $a[0];
			
			// Cleanup
			mysql_free_result($result);
		}
		
		// Close
		mysql_close($db);
		
		// Return it
		return $scalar;		
	}

	function InsertRow($sqlrow, $tblname)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;
		
		// Execute
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		$query = GetInsertRowString($sqlrow, $tblname);
		$result = mysql_query($query, $db);
		$id = mysql_insert_id($db);
		DisplaySqlError($db, $query);
		mysql_close($db);
		
		// Return the insert id
		return $id;
	}

	function UpdateRow($sqlrow, $tblname, $idcol)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;

		// Execute
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		$query = GetUpdateRowString($sqlrow, $tblname, $idcol);
		$result = mysql_query($query, $db);
		DisplaySqlError($db, $query);
		mysql_close($db);
	}

	function DeleteRow($query)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;
	
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		$result = mysql_query($query, $db);
		DisplaySqlError($db, $query);
		mysql_close($db);
	}

	function SqlExecute($query)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;
		
		// Execute
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		$result = mysql_query($query, $db);
		DisplaySqlError($db, $query);
		mysql_close($db);
	}

	function GetInsertRowString($sqlrow, $tblname)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;
		
		// Get the stuff
		$aKeys = array_keys($sqlrow->m_sData);
		$aValues = array_values($sqlrow->m_sData);
		
		// Generate the query
		$query = "INSERT INTO ".$tblname."(";

		// Run each value
		$names = "";
		for($idx = 0; $idx < count($aKeys); $idx++ ) {
			if($names != "") $names = $names.",";
			$names = $names.$aKeys[ $idx ];
		}
			
		// Center
		$query = $query.$names.") VALUES(";

		// Run each value
		$values = "";
		for($idx = 0; $idx < count($aValues); $idx++ ) {
			$aValues[ $idx ] = SQLEncode($aValues[ $idx ]);
			if($values != "") $values = $values.",";
			$values = $values."'".$aValues[ $idx ]."'";
		}
		
		// Finish
		return $query.$values.")";
	}

	function GetUpdateRowString($sqlrow, $tblname, $idcol)
	{
		global $gblMySqlUser;
		global $gblMySqlPass;
		global $gblMySqlDb;

		// Get the stuff
		$aKeys = array_keys($sqlrow->m_sData);
		$aValues = array_values($sqlrow->m_sData);
		
		// Generate the query
		$query = "UPDATE ".$tblname." SET";
		$values = "";

		// Run each value
		for($idx = 0; $idx < count($aKeys); $idx++ )
		{
			if(!is_numeric($aKeys[ $idx ]))
			{
				$aValues[ $idx ] = SQLEncode($aValues[ $idx ]);
				if($values != "" ) $values = $values.",";
				$values = $values." ".$aKeys[ $idx ]." = '".$aValues[ $idx ]."'";
			}
		}

		// Set the id
		$idval = SQLEncode($sqlrow->GetValue($idcol));
		$query = $query.$values." WHERE ".$idcol." = '".$idval."'";

		// Execute
		return $query;
	}
	
	function SQLEncode($text)
	{
		return mysql_escape_string($text);
	}

	function DisplaySqlError($db, $query)
	{
		global $displayMySqlErrors;
		$mySqlError = mysql_error($db);
		if($mySqlError != null && $mySqlError != "" && $displayMySqlErrors = 1)
		{
			echo "db error=".$mySqlError."<hr><!--\n\n".$query."\n\n-->";
			exit;
		}
	}
	
?>