299 lines
7.1 KiB
PHP
299 lines
7.1 KiB
PHP
<?php
|
|
|
|
if (!isset($db_host)) exit();
|
|
if (!isset($db_uname)) exit();
|
|
if (!isset($db_pass)) exit();
|
|
if (!isset($db_dbname)) exit();
|
|
|
|
$SQLlog = NULL;
|
|
$queryLog = NULL;
|
|
|
|
|
|
// sql query function to write to the sql log
|
|
if (!function_exists('db_logQuery')) {
|
|
function db_logQuery($query = '') {
|
|
if (!isset($queryLog) || !is_resource($SQLlog)) {
|
|
$queryLog = fopen("/m/data1/app.hotlunches.net/sql_query.log","a");
|
|
}
|
|
|
|
fwrite($queryLog,"==========================================\n");
|
|
|
|
$date = date("Y-m-d H:i:s",time());
|
|
fwrite($queryLog,"$date\n");
|
|
|
|
if (strlen($query)) {
|
|
fwrite($queryLog,$query."\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// sql query function to write to the error log
|
|
if (!function_exists('db_writeLog')) {
|
|
function db_writeLog($eMessage, $localMessage = '', $query = '') {
|
|
global $db_dbname;
|
|
|
|
if (!isset($SQLlog) || !is_resource($SQLlog)) {
|
|
$SQLlog = fopen("/m/data1/app.hotlunches.net/sql_error.log","a");
|
|
}
|
|
|
|
fwrite($SQLlog,"==========================================\n");
|
|
|
|
$date = date("Y-m-d H:i:s",time());
|
|
fwrite($SQLlog,"$date\n");
|
|
|
|
fwrite($SQLlog,"DB: $db_dbname\n");
|
|
|
|
fwrite($SQLlog,$eMessage."\n");
|
|
|
|
if (strlen($localMessage)) {
|
|
fwrite($SQLlog,$localMessage."\n");
|
|
}
|
|
|
|
if (strlen($query)) {
|
|
fwrite($SQLlog,$query."\n");
|
|
}
|
|
|
|
exit();
|
|
}
|
|
}
|
|
|
|
|
|
//connect to the database..
|
|
if (!function_exists('db_connect')) {
|
|
function db_connect($db_host,$db_uname,$db_pass,$db_dbname) {
|
|
global $_DB;
|
|
|
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
|
|
|
try {
|
|
$_DB = new mysqli($db_host,$db_uname,$db_pass,$db_dbname);
|
|
}
|
|
catch (mysqli_sql_exception $e) {
|
|
$eMessage = $e->getMessage();
|
|
$localMessage = "Cannot connect to database host: $db_host :: $db_dbname";
|
|
db_writeLog($eMessage, $localMessage);
|
|
exit();
|
|
}
|
|
|
|
if ($_DB->connect_errno) {
|
|
$eMessage = $_DB->connect_error;
|
|
$localMessage = "Cannot connect to database host: $db_host :: $db_dbname";
|
|
db_writeLog($eMessage, $localMessage);
|
|
exit();
|
|
}
|
|
|
|
$res = $_DB->set_charset("utf8");
|
|
if (!$res) {
|
|
$eMessage = "Cannot set character set: " . $_DB->error;
|
|
printf($eMessage);
|
|
db_writeLog($eMessage);
|
|
exit();
|
|
}
|
|
}
|
|
}
|
|
|
|
// execute database connect
|
|
db_connect($db_host,$db_uname,$db_pass,$db_dbname);
|
|
|
|
|
|
|
|
// Define error-handling sql-query function
|
|
if (!function_exists('db_query')) {
|
|
function db_query($query) {
|
|
global $_DB, $school_num;
|
|
|
|
if (!is_object($_DB)) return;
|
|
if (strlen($query) == 0) return(-1);
|
|
// db_logQuery($query);
|
|
|
|
try {
|
|
$r = $_DB->query($query);
|
|
} catch (mysqli_sql_exception $e) {
|
|
$Script = "";
|
|
if (is_array($_SERVER) && array_key_exists('PHP_SELF',$_SERVER)) $Script = $_SERVER['PHP_SELF'];
|
|
if (strlen($Script)) $Script = " ($Script)";
|
|
if (array_key_exists('action', $GLOBALS)) $Script .= ' '.$GLOBALS['action'];
|
|
if (array_key_exists('naction', $GLOBALS)) $Script .= ' / '.$GLOBALS['naction'];
|
|
|
|
$eMessage = $e->getMessage();
|
|
$localMessage = "SQL Query Error$Script";
|
|
db_writeLog($eMessage, $localMessage, $query);
|
|
}
|
|
|
|
if (isset($r) && is_object($r)) return($r);
|
|
if (!isset($r) || !$r) {
|
|
|
|
$date = date("Y-m-d H:i:s",time());
|
|
$msg = "";
|
|
$msg .= "$date: $query\n";
|
|
$msg .= "$date: " . $_DB->error . "\n";
|
|
|
|
$file = basename($_SERVER['SCRIPT_FILENAME']);
|
|
$msg .= "$date: $file\n";
|
|
|
|
if (isset($GLOBALS['action'])) $msg .= "$date: action - " . $GLOBALS['action'] . "\n";
|
|
if (isset($GLOBALS['naction'])) $msg .= "$date: naction - " . $GLOBALS['naction'] . "\n";
|
|
|
|
if (isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])) {
|
|
$msg .= "$date: Host - " . $_SERVER['HTTP_HOST'] . "\n";
|
|
$msg .= "\n";
|
|
db_writeLog($msg, '', $query);
|
|
|
|
header("Location: /?action=&error_message=".urlencode("Database Error"));
|
|
}
|
|
else {
|
|
$msg .= "\n";
|
|
db_writeLog($msg, '', $query);
|
|
writeLog("DB ERROR - $date\n");
|
|
writeLog("$msg\n");
|
|
}
|
|
exit();
|
|
}
|
|
return(true);
|
|
}
|
|
}
|
|
|
|
|
|
// sql query function to return a single row (numeric indices)
|
|
if (!function_exists('db_getVal')) {
|
|
function db_getVal($query) {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
|
|
$r = db_query($query);
|
|
if (is_object($r) && db_num_rows($r) == 1) {
|
|
// $data = $r->fetch_array(MYSQLI_NUM);
|
|
$data = db_fetch_assoc($r);
|
|
if (count($data) == 1) $data = implode($data);
|
|
}
|
|
else {
|
|
$data = '';
|
|
}
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
// sql query function to return a single row (associative indices)
|
|
if (!function_exists('db_getArray')) {
|
|
function db_getArray($query) {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
|
|
$r = db_query($query);
|
|
if (is_object($r) && db_num_rows($r) == 1) {
|
|
$data = $r->fetch_array(MYSQLI_ASSOC);
|
|
if (count($data) == 1) $data = implode($data);
|
|
}
|
|
else {
|
|
$data = '';
|
|
}
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_fetch_assoc')) {
|
|
function db_fetch_assoc($results) {
|
|
if (!is_object($results)) return(null);
|
|
return($results->fetch_assoc());
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_fetch_array')) {
|
|
function db_fetch_array($results) {
|
|
if (!is_object($results)) return(null);
|
|
return($results->fetch_array(MYSQLI_BOTH));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_num_rows')) {
|
|
function db_num_rows($results) {
|
|
if (!is_object($results)) return(0);
|
|
return($results->num_rows);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_escape_string')) {
|
|
function db_escape_string($string) {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
return($_DB->real_escape_string($string));
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_affected_rows')) {
|
|
function db_affected_rows() {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
return($_DB->affected_rows);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_insert_id')) {
|
|
function db_insert_id() {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
return($_DB->insert_id);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_error')) {
|
|
function db_error() {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
return($_DB->error);
|
|
}
|
|
}
|
|
|
|
|
|
// check if table exists
|
|
if (!function_exists('db_tableExists')) {
|
|
function db_tableExists($table) {
|
|
$r = db_query("SHOW TABLES LIKE '$table'");
|
|
if (db_num_rows($r) == 1) return(true);
|
|
else return(false);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_columnExists')) {
|
|
function db_columnExists($table,$column) {
|
|
$r = db_query("SHOW COLUMNS FROM $table LIKE '$column'");
|
|
return((db_num_rows($r))?TRUE:FALSE);
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('db_close')) {
|
|
function db_close() {
|
|
global $_DB;
|
|
|
|
if (!is_object($_DB)) return;
|
|
$r = $_DB->close();
|
|
return($r);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Set collation
|
|
db_query("SET NAMES utf8");
|
|
db_query("SET CHARACTER SET utf8");
|
|
// $_DB->set_charset('utf8');
|
|
|
|
?>
|