/*
##############################################
#
# Filename : mysql.class
#
# Created : 02/07/03 00:40 GMT
#
# Created By : Syberius
#
# Version : 1.00
#
##############################################
#
# This MySQL Class allows an author to easily run SQL queries
# and catch/log errors in case of failure. While there are other
# classes that do this, I use my own because it was built for
# projects of mine that needed it, however it has been adapted
# for general use
#
##############################################
#
# Additions and modifications are allowed as long as this message
# remains. If you do make changes please contact me at this address.
#
# syberius@tiscali.co.uk
#
# To inform me of any changes to this file.
#
#
##############################################
#
# How to use:
#
# Simply include this file using
#
# include("mysql.class");
#
# and add the following lines of code on each page the class is called
#
# $sql_class = new sql_queries();
# $sql_class -> do_mysql_connect();
#
# To use simply use a simple SQL query and pass it to one of the
# functions like this:
#
# $sql = "SELECT * FROM some_table WHERE 1"'
# $res = $sql_class -> run_sql_query($sql);
#
# This will return the MySQL result as normal or show an error on
# failure. The other 2 functions are:
#
# $array = $sql_class -> return_sql_array($sql);
# $num = $sql_class -> return_sql_rows($sql);
#
# The first returns an array of fields from a given query and the
# second return the number of rows from a given query.
#
##############################################
*/
class sql_queries
{
var $mysql;
var $mysql_connect;
function define_settings()
{
/*
This is where the settings for MySQL and error handling are set.
This information must be correct or the class will not work.
host: This is the server that mysql is on, leave as localhost in most cases!
user: The username used for your mysql account.
pass: The password used for your mysql account.
db: this is the database you want to connect to, if you will be using more than one, set this to the primary db!
*/
$this -> mysql['host'] = "";
$this -> mysql['user'] = "";
$this -> mysql['pass'] = "";
$this -> mysql['db'] = "";
/*
In the event of an error you can output custom messages to the user.
The first 2 errors will only appear if the script cannot connect,
the other setting is for general errors.
*/
$this -> mysql['con_error'] = "MySQL encountered a severe error while trying to connect to the hostn";
$this -> mysql['db_error'] = "MySQL encountered a severe error while trying to use the databasen";
$this -> mysql['gen_error'] = "MySQL Error: Due to a database error this page cannot be displayedn";
/*
If an error does occur a log file will write the error to file for debugging and error checking.
Should you wish to put the file in a sub directory, chmod it to 777 along with the file.
*/
$this -> mysql['log_file'] = "mysql_errors.log";
}
function geefFH_DB()
{
$this -> define_settings();
$connection = @mysql_connect($this -> mysql['host'], $this -> mysql['user'], $this -> mysql['pass']);
// connection made ?
if( $connection )
{
// select the database
if( !mysql_select_db($this -> mysql['db'], $connection ) )
{
// could not select database!
die('Could not select the database! Error: ' . mysql_error() );
}
}
// could not connect to the database
else
{
die('Could not make a connection to the database! Error: ' . mysql_error() );
}
return $connection;
}
// !! DO NOT EDIT PAST THIS POINT !! //
function do_mysql_connect()
{
$this -> define_settings();
$mysql_connect = @mysql_pconnect($this -> mysql['host'], $this -> mysql['user'], $this -> mysql['pass']);
$mysql_database = @mysql_select_db($this -> mysql['db']);
if (!$mysql_connect)
{
echo "
n";
echo $this -> mysql['con_error'];
$sql_class = new sql_queries();
$sql_error = mysql_error();
$sql_log = $sql_class -> log_sql_error("mysql_pconnect", $sql_error);
}
elseif (!$mysql_database)
{
echo "
n";
echo $this -> mysql['db_error'];
$sql_class = new sql_queries();
$sql_error = mysql_error();
$sql_log = $sql_class -> log_sql_error("mysql_select_db", $sql_error);
}
}
function run_sql_query($sql_code)
{
$sql_result = @mysql_query($sql_code);
if (!$sql_result)
{
$sql_error = @mysql_error();
//echo "
n";
//echo "
".$this -> mysql['gen_error']."
n"; $this -> log_sql_error($sql_code, $sql_error); return(false); } else { return ($sql_result); } } function return_sql_array($sql_code) { $sql_result = @mysql_query($sql_code); if (!$sql_result) { $sql_error = mysql_error(); //echo "".$this -> mysql['gen_error']."
n"; $this -> log_sql_error($sql_code, $sql_error); return(false); } else { $sql_array = @mysql_fetch_array($sql_result); return ($sql_array); } } function retrun_sql_rows($sql_code) { $sql_result = @mysql_query($sql_code); if (!$sql_result) { $sql_error = @mysql_error(); echo "".$this -> mysql['gen_error']."
n"; $this -> log_sql_error($sql_code, $sql_error); return(false); } else { $sql_num_rows = @mysql_num_rows($sql_result); return ($sql_num_rows); } } function log_sql_error($sql_code, $sql_error) { // $this -> define_settings(); /* if ($log_file = fopen($this -> mysql['log_file'], "a")) { $message = "[".$_SERVER["REMOTE_ADDR"]."] "."MySQL query ".$sql_code." produced this error: $sql_error"; fwrite($log_file, date("[d-m-Y H:i:s] ") . $message . "n"); fclose($log_file); exit; } */ } } ?>