1<?php
2
3global  $db;		// Make sure it IS global, regardless of our context
4
5if (!isset($mysqlport)){
6	$mysqlport = 3306;
7}
8
9$db = mysqli_connect($host , $DBUser, $DBPassword,$DatabaseName, $mysqlport);
10
11//this statement sets the charset to be used for sending data to and from the db server
12//if not set, both mysql server and mysql client/library may assume otherwise
13
14mysqli_set_charset($db, 'utf8');
15
16
17if ( !$db ) {
18	echo '<br />' . _('The configuration in the file config.php for the database user name and password do not provide the information required to connect to the database server');
19	session_unset();
20	session_destroy();
21	echo '<p>' . _('Click') . ' ' . '<a href="index.php">' . _('here') . '</a>' . ' '  ._('to try logging in again') . '</p>';
22
23	exit;
24}
25
26if (isset($DatabaseName)) {
27	if (!mysqli_select_db($db,$DatabaseName)) {
28		echo '<br />' . _('The company name entered does not correspond to a database on the database server specified in the config.php configuration file. Try logging in with a different company name');
29		unset ($DatabaseName);
30		exit;
31	}
32} else {
33	if (!mysqli_select_db($db,$_SESSION['DatabaseName'])) {
34		echo '<br />' . _('The company name entered does not correspond to a database on the database server specified in the config.php configuration file. Try logging in with a different company name');
35		unset ($_SESSION['DatabaseName']);
36		exit;
37	}
38}
39
40require_once ($PathPrefix .'includes/MiscFunctions.php');
41
42//DB wrapper functions to change only once for whole application
43
44function DB_query ($SQL,
45		$ErrorMessage='',
46		$DebugMessage= '',
47		$Transaction=false,
48		$TrapErrors=true){
49
50	global $debug;
51	global $PathPrefix;
52	global $db;
53
54
55	$result=mysqli_query($db, $SQL);
56
57	$_SESSION['LastInsertId'] = mysqli_insert_id($db);
58
59	if ($DebugMessage == '') {
60		$DebugMessage = _('The SQL that failed was');
61	}
62
63	if (DB_error_no() != 0 AND $TrapErrors==true){
64
65		message_log($ErrorMessage . '<br />' . DB_error_msg($db),'error');
66		if ($debug==1){
67			message_log($DebugMessage. '<br />' . $SQL . '<br />','error');
68		}
69		if ($Transaction){
70			$SQL = 'rollback';
71			$Result = DB_query($SQL);
72			if (DB_error_no() !=0){
73				message_log(_('Error Rolling Back Transaction'), 'error');
74			}
75		}
76	}
77
78	return $result;
79}
80
81function DB_fetch_row (&$ResultIndex) {
82	$RowPointer=mysqli_fetch_row($ResultIndex);
83	Return $RowPointer;
84}
85
86function DB_fetch_assoc (&$ResultIndex) {
87
88	$RowPointer=mysqli_fetch_assoc($ResultIndex);
89	Return $RowPointer;
90}
91
92function DB_fetch_array (&$ResultIndex) {
93	$RowPointer=mysqli_fetch_array($ResultIndex);
94	Return $RowPointer;
95}
96
97function DB_data_seek (&$ResultIndex,$Record) {
98	mysqli_data_seek($ResultIndex,$Record);
99}
100
101function DB_free_result (&$ResultIndex){
102	mysqli_free_result($ResultIndex);
103}
104
105function DB_num_rows (&$ResultIndex){
106	return mysqli_num_rows($ResultIndex);
107}
108function DB_affected_rows(&$ResultIndex){
109	global $db;
110	return mysqli_affected_rows($db);
111}
112function DB_error_no (){
113	global $db;
114	return mysqli_errno($db);
115}
116
117function DB_error_msg(){
118	global $db;
119	return mysqli_error($db);
120}
121function DB_Last_Insert_ID($Table, $FieldName){
122//	return mysqli_insert_id($Conn);
123	if (isset($_SESSION['LastInsertId'])) {
124		$Last_Insert_ID = $_SESSION['LastInsertId'];
125	} else {
126		$Last_Insert_ID = 0;
127	}
128//	unset($_SESSION['LastInsertId']);
129	return $Last_Insert_ID;
130}
131
132function DB_escape_string($String){
133	global $db;
134	return mysqli_real_escape_string($db, htmlspecialchars($String, ENT_COMPAT,'utf-8', false));
135}
136function DB_Txn_Begin(){
137	global $db;
138	$result=mysqli_query($db,"BEGIN");
139}
140function DB_Txn_Commit(){
141	global $db;
142	$result=mysqli_query($db,"COMMIT");
143}
144
145?>
146