<?php
	//remarks.php functions using database-specific mysql functions
	function quote_smart($value)
	{  		
   		if (get_magic_quotes_gpc()) 
       		$value = stripslashes($value); // Stripslashes if input already escaped
		  		
   		if (!is_numeric($value)) 
		{
			if (!function_exists('mysql_real_escape_string')) 
				$value = "'" . mysql_escape_string($value) . "'";
			else
       			$value = "'" . mysql_real_escape_string($value) . "'"; // Quote if not a number or a numeric string
		}
		return $value;
	}

	function doQuery($query,$location,&$sql) 
	//'&' in the argument means the argument is passed by reference so that
	//  the value of $error is passed back to the calling code
	{
		$sql=mysql_query($query);
		$error=false;
		$error=reportError($location,mysql_errno(), mysql_error() );
		return($error);
	}

	function fetchArray($sql)
	//returns the next row in $sql
	{
		$row=mysql_fetch_array($sql);
		return $row;
	}

	function freeResult($sql)
	//frees the result set from the database
	{
		mysql_free_result($sql);
	}

	function connectToDatabase()
	{
		$connect = mysql_connect(DB_URL, DB_USERNAME, DB_PASSWORD) or die("Cannot connect to database: ".mysql_error());
		mysql_select_db(DB_NAME,$connect) or die("Cannot select database: ".mysql_error());
			
		// create database table if needed	
		$table = getTableName();
		mysql_query("create table if not exists ".$table." (id int not null AUTO_INCREMENT, imageID char(200), date datetime not null default '0000-00-00 00:00:00', name char(100), msg text, ip1 char(100), approved char(1) default 'Y', refer char(255), primary key (id))");
		if( mysql_errno()!=0 )
			echo( reportError("Error Creating Table $table", mysql_errno(), mysql_error()) );

		//check if old-style table, update if needed
		$query = "select count(*) from $table where approved='Y'" ;
		$sql=mysql_query($query);
		if( mysql_errno()!=0 ) //'approved' column does not exist, so old style table
		{ //change column name and mark all remarks as 'approved'
			$query="Alter table $table change ip2 approved char(1)";
			$error = doQuery($query,"Alter table",$sql);
			$query="update $table SET approved= 'Y'";
			$error = doQuery($query,"Update new table to 'approved' status",$sql);
		}	
		
	}


?>
