<?php

	define (DB_HOST, "localhost");
	define (DB_NAME, "drcassil_drcdb");
	define (DB_USER, "drcassil_dbuser");
	define (DB_PASS, "userdrc69");
	
	if (!strcmp(DB_HOST,"CHANGE_THIS"))
	{
		echo "You must setup your database settings. Change the defines DB_HOST, DB_NAME, DB_USER and DB_PASS\n";
		exit;
	}
	
	/*
	 * SelectQuery
	 * Desc: Retrieve data from the database.
	 * Parms: 
	 *   $tables - comma separated list of table names.
	 *   $fields - comma separated list of field names or "*".
	 *   $where - SQL Where clause (e.g. "where id=2").
	 *   $groupBy - SQL Group clause (e.g. "group by name").
	 *   $orderBy - SQL Order clause (e.g. "order by name").
	 *   $show_debug - If true then print SQL query.
	 * Returns: 
	 *   2d array of rows and columns on success.
	 *   Error String on failure.
	 */
	Function SelectQuery ($tables, $fields, $where="", $groupBy="", $orderBy="", $show_debug=false)
	{
		$db = mysql_connect(DB_HOST, DB_USER, DB_PASS);
		if ($db == false)
		{
			return mysql_errno().": ".mysql_error()."<br>";
		}
	
		// Return the data requested by the fields, tables and where.
		// Return the data in a 2 dimensional array.
		$values = array();
		$field_array = split (", ?", $fields);
		
		$max_fields = ($fields == "*")? 20 : count($field_array);
		
		if (!empty($where))
		{
			if (!strstr(strtolower($where),"where ")) $where = "where $where";
		}
		$query = "select $fields from $tables $where $groupBy $orderBy";
		if ($show_debug == true) echo "query=$query<br>\n";
		$stmt = mysql_db_query (DB_NAME, $query, $db);
		if ($stmt == false)
		{
			return mysql_errno().": ".mysql_error()."<br>"; 
		}
		while ($fields = mysql_fetch_row ($stmt))
		{
			$values[] = $fields;
		}
		@mysql_free_result ($stmt);
		@mysql_close($db);
		
		return $values;
	}
	
	/********************************************************/
	
	/*
	 * InsertQuery
	 * Desc: Insert data into the database.
	 * Parms: 
	 *   $tableName - database table name.
	 *   $values - associative array of field names and corresponding values.
	 *   $debug - If true then return SQL query without executing.
	 * Returns: 
	 *   Nothing on success.
	 *   Error String on failure.
	 */
	Function InsertQuery ($tableName, $values, $debug=false)
	{
		/* Insert the $values into the database.
		 * e.g. 
		 * $values = array ("name"=>"kris","email"=>"karn@nucleus.com");
		 * InsertQuery ("employee", $values);
		*/
		return InsertUpdateQuery ("", $tableName, $values, $debug);
	}
	
	/********************************************************/
	
	/*
	 * UpdateQuery
	 * Desc: Update data in the database.
	 * Parms: 
	 *   $tableName - database table name.
	 *   $values - associative array of field names and corresponding values.
	 *   $where - SQL Where clause to specify which row(s) to update.
	 *   $debug - If true then return SQL query without executing.
	 * Returns: 
	 *   Nothing on success.
	 *   Error String on failure.
	 */
	Function UpdateQuery ($tableName, $values, $where="", $debug=false)
	{
		/* Update the $values in the database.
		 * e.g. 
		 * $values = array ("name"=>"kris","email"=>"karn@nucleus.com");
		 * $where = "WHERE id='1'";
		 * UpdateQuery ("employee", $values, $where);
		*/
		if (empty($where)) $where = " ";
		return InsertUpdateQuery ($where, $tableName, $values, $debug);
	}
	
	/********************************************************/
	
	Function InsertUpdateQuery ($type, $tableName, $fieldValues, $debug=false)
	{
		$db = mysql_connect(DB_HOST, DB_USER, DB_PASS);
		if ($db == false)
		{
			return mysql_errno().": ".mysql_error()."<br>";
		}
		
		$i = 0;
		$fields = "";
		$values = "";
		$updateList = "";
		
		while (list ($key, $val) = each ($fieldValues))
		{
			if ($i > 0)
			{
				$fields .= ", ";
				$values .= ", ";
				$updateList .= ", ";
			}
	
			$fields .= $key;
	
			// If you do not want to add quotes
			// around the field then specify
			// /*NO_QUOTES*/ when passing in the value.
			// For update statements like 
			// "update poll set total_votes=total_votes+1", 
			// you do not want
			// the value field to have quotes around it.
			if (strstr($val,"/*NO_QUOTES*/"))
			{
				$val = str_replace ("/*NO_QUOTES*/", "", $val);
				$updateList .= "$key=$val";
				$values .= $val;
			}
			else
			{
				$updateList .= "$key='$val'";
				$values .= "'$val'";
			} 
	
			$i++;
	
		}
	
		if (empty($type))
		{
			$query = "insert into $tableName ($fields) values ($values)";
		}
		else
		{
			$query = "update $tableName set $updateList $type";
		}
		
		if ($debug)
		{
			@mysql_close($db);
			return $query;
		}
		
		$stmt = mysql_db_query (DB_NAME, $query, $db);
		
		if (!$stmt)
		{
			$error = mysql_error ($db);
			
			if (strstr($error, "Duplicate entry"))
			{
				$error = "Field value already in use, choose another.<br>\n";
			}
			
		}
		@mysql_free_result ($stmt);
		@mysql_close($db);
		
		return $error;
	}

	/*
	 * DeleteQuery
	 * Desc: Delete data from the database.
	 * Parms: 
	 *   $tableName - database table name.
	 *   $where - SQL Where clause to specify which row(s) to delete.
	 *   $debug - If true then return SQL query without executing.
	 * Returns: 
	 *   Nothing on success.
	 *   Error String on failure.
	 */
	Function DeleteQuery ($tableName, $where="", $debug=false)
	{
		$db = mysql_connect(DB_HOST, DB_USER, DB_PASS);
		if ($db == false)
		{
			return mysql_errno().": ".mysql_error()."<br>";
		}
		
		// Delete a row from the specified table.
		if (!empty($where))
		{
			if (!strstr(strtolower($where),"where ")) $where = "where $where";
		}
		
		$query = "delete from $tableName $where";
		
		if ($debug)
		{
			@mysql_close($db);
			return $query;
		}
		
		$stmt = mysql_db_query (DB_NAME, $query, $db);
		
		if (!$stmt)
		{
			$error = mysql_error ($db);
		}
		
		@mysql_free_result ($stmt);
		@mysql_close($db);
		
		return $error;
	}

?> 
