Location: PHPKode > projects > PHP-Comics > php-comics/inc/database.class.php
<?php
abstract class DB
{
	private static $connection;
	private static $settings;
	public static function Query($query,$debug = false)
	{
		if(DB::$connection == null)
		{

			DB::$connection = mysql_connect(MYSQL_HOST,MYSQL_USERNAME,MYSQL_PASSWORD);
		}
		$result = mysql_db_query(MYSQL_DATABASE,$query);
		if($debug)
		{
			echo $query;
			echo mysql_error();
		}
		return $result;
	}
	public static function QueryResult($query,$resultof = "ID",$debug = false)
	{
		$result = DB::Query($query,$debug);
		if(mysql_num_rows($result)!= 0)
		{
			return mysql_result($result,0,$resultof);		
		}
		return null;
	}
	private static function checksetting($Setting,$UID)
	{
		if(!is_array(DB::$settings))
		{
			DB::$settings = array();
		}
		else
		{
			if(isset(DB::$settings[$Setting][$UID]))
			{
				return DB::$settings[$Setting][$UID];
			}
		}
		return false;		
	}
	public static function setSetting($Setting,$Value,$UID = 0)
	{
		DB::Query("INSERT INTO Setting (KeyName, UID, Value) VALUES ('$Setting',$UID,'".addslashes($Value)."') ON DUPLICATE KEY UPDATE Value = '".addslashes($Value)."'");
		if(!is_array(DB::$settings))
		{
			DB::$settings = array();
		}
		DB::$settings[$Setting][$UID] = $Value;
	}
	public static function Setting($Setting, $UID = NULL)
	{
		if(empty($UID))
		{
			global $_SESSION;
			$UID = $_SESSION['UID'];
		}
		$settingcache = DB::checksetting($Setting,$UID);
		if(!($settingcache === false))
		{
			return $settingcache;	
		}
		$result = DB::Query("SELECT Value FROM Setting WHERE KeyName = '$Setting' AND UID = $UID LIMIT 1");
		if(mysql_num_rows($result)!=0)
		{
		
			$value = mysql_fetch_array($result);
			DB::$settings[$Setting][$UID];		
			return $value['Value'];
		}
		else
		{
			$value = DB::QueryResult("SELECT Value FROM Setting WHERE KeyName = '$Setting' AND UID = 0","Value");
			DB::Query("INSERT INTO Setting (KeyName,Value,UID) VALUES ('$Setting',$value,$UID)");
			return $value;			
		}
	}
	public static function QueryToArray($query,$indexby = 0,$debug = false)
	{
		$result = DB::Query($query,$debug);
		$resultarray = array();
		$counter = 0;
		while($row = mysql_fetch_array($result))
		{
			if(empty($indexby))
			{
				$resultarray[$counter++] = $row;
			}
			else
			{
				$resultarray[$row[$indexby]] = $row;
			}
		}
		return $resultarray;
	}
}
?>
Return current item: PHP-Comics