<?php
// db.php
// This file is used to connect to the MySQL database
// It also includes some useful/common database functions.

// hard-coded parameters: replace as needed for your setup
$dbhost = "localhost";
$dbuser = "peter";
$dbpass = "i4mt3rr0r";

// dbConnect() function: called prior to making a query
function dbConnect($db="Cody") {
	global $dbhost, $dbuser, $dbpass;
	$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
	or die("The site database appears to be down.");
	if ($db!="" and !@mysql_select_db($db))
	die("The site database is unavailable.");
	return $dbcnx;
}

// The dbparse function will format input to help
// prevent SQL injection
function dbparse($value){
	// Stripslashes
	if (get_magic_quotes_gpc()) {
		$value=stripslashes($value);
	}
	// Quote if not integer
	if (!is_numeric($value)) {
		$value = "'" . mysql_real_escape_string($value) . "'";
	}
	return $value;
}

// The execsql function is how we call queries.
// NOTE: every query I use is designed to return some values.
// Null or empty recordsets will return an error.
// Also note the hack around MySQL's inability to return user-defined
// error codes from stored routines (the 'unknown table' hack)
function execsql($qry){
	$result = mysql_query($qry);
	return $result;
}


