<?
error_reporting(0);



############ Website Database Communication Functions##########
function connect_db() {
	global $ARR_CFGS;
	if (!isset($GLOBALS['dbcon'])) {
		$GLOBALS['dbcon'] = mysqli_connect($ARR_CFGS["db_host"], $ARR_CFGS["db_user"], $ARR_CFGS["db_pass"],$ARR_CFGS["db_name"]);
		
		if(!$GLOBALS['dbcon'])
		{
		 die("Could not connect to database. Please check configuration and ensure MySQL is running.");
		}
		
	}
}


function db_query($sql, $dbcon2 = null) {
	if($dbcon2==''){
		if(!isset($GLOBALS['dbcon'])) {
			connect_db();
		}
		$dbcon2	= $GLOBALS['dbcon'];
	}
	$time_before_sql = checkpoint();
	$result	= mysqli_query($dbcon2,$sql) or	die(db_error($sql));
	return $result;
}


function db_scalar($sql, $dbcon2 = null) {
	if($dbcon2==''){
		if(!isset($GLOBALS['dbcon'])) {
			connect_db();
		}
		$dbcon2	= $GLOBALS['dbcon'];
	}
	$result	= db_query($sql, $dbcon2);
	if ($line =	mysqli_fetch_array($result))	{
		$response =	$line[0];
	}
	return $response;
}


function db_error($sql) {
	echo "<div style='font-family: tahoma; font-size: 11px; color: #333333'><br>".mysqli_error()."<br>";
	print_error();
	if(LOCAL_MODE) {
		echo "<br>sql: $sql";
	}
	echo "</div>";
}


function print_error() {
	$debug_backtrace = debug_backtrace();
	for ($i = 1; $i < count($debug_backtrace); $i++) {
		$error = $debug_backtrace[$i];
		echo "<br>";
		echo "<div>";
		echo "<b>File:</b> ".$error['file']."<br>";
		echo "<b>Line:</b> ".$error['line']."<br>";
		echo "<b>Function:</b> ".$error['function']."<br>";
		echo "</div>";
	}
}


function db_fetch_array($sql, $dbcon2 = null) {
	if($dbcon2==''){
		if(!isset($GLOBALS['dbcon'])) {
			connect_db();
		}
		$dbcon2	= $GLOBALS['dbcon'];
	}
	$result	= mysqli_fetch_array($sql) or	die(mysqli_error());
	return $result;
}


function db_fetch_object($sql, $dbcon2 = null) {
	if($dbcon2==''){
		if(!isset($GLOBALS['dbcon'])) {
			connect_db();
		}
		$dbcon2	= $GLOBALS['dbcon'];
	}
	$result	= mysqli_fetch_object($sql) or	die(mysqli_error());	
	return $result;
}


function NumRows($sql, $dbcon2 = null) {
	if($dbcon2==''){
		if(!isset($GLOBALS['dbcon'])) {
			connect_db();
		}
		$dbcon2	= $GLOBALS['dbcon'];
	}
	$result	= mysqli_num_rows($sql) ;
	if(!is_numeric($result)){
		return 0;
	}
	else{
		return $result;
	}
}
?>