-
Notifications
You must be signed in to change notification settings - Fork 41
/
preheader.php
232 lines (198 loc) · 5.62 KB
/
preheader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/* you SHOULD edit the database details below; fill in your database info */
#this is the info for your database connection
####################################################################################
##
$MYSQL_HOST = "localhost";
$MYSQL_LOGIN = "root";
$MYSQL_PASS = "";
$MYSQL_DB = "ajaxcrud_demos";
##
$LOCAL_JS = FALSE; // FALSE for inclusion of remote js files
##
####################################################################################
/********* THERE SHOULD BE LITTLE NEED TO EDIT BELOW THIS LINE *******/
####################################################################################
#a session variable is set by class for much of the CRUD functionality -- eg adding a row
session_start();
#for pesky IIS configurations without silly notifications turned off
error_reporting(E_ALL - E_NOTICE);
$useMySQLi = true;
if (!class_exists("mysqli")){
$useMySQLi = false; //mysqli is not enabled on this server; fallback to using mysql
}
if ($useMySQLi){
$mysqliConn = new mysqli($MYSQL_HOST, $MYSQL_LOGIN, $MYSQL_PASS, $MYSQL_DB);
/* check connection */
if (mysqli_connect_errno()) {
//logError("Connect failed in getMysqli(): ", mysqli_connect_error());
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqliConn->set_charset("utf8");
}
else{
/*
use this connection if your hosting config does NOT support mysqli
this code was for mySQL connections; was replaced in v8.6 with mysqli
*/
$db = @mysql_connect($MYSQL_HOST,$MYSQL_LOGIN,$MYSQL_PASS);
if(!$db){
echo('Unable to authenticate user. <br />Error: <b>' . mysql_error() . "</b>");
exit;
}
$connect = @mysql_select_db($MYSQL_DB);
if (!$connect){
echo('Unable to connect to db <br />Error: <b>' . mysql_error() . "</b>");
exit;
}
mysql_query("SET NAMES 'utf8'");
//mysql_query("SET character_set_results = 'utf8_general_ci', character_set_client = 'utf8_general_ci', character_set_connection = 'utf8_general_ci', character_set_database = 'utf8_general_ci', character_set_server = 'utf8_general_ci'", $db);
}
# what follows are custom database handling functions - required for the ajaxCRUD class
# ...but these also may be helpful in your application(s) :-)
if (!function_exists('q')) {
function q($q, $debug = 0){
global $mysqliConn, $useMySQLi;
if ($useMySQLi){
if (!($r = $mysqliConn->query($q))) {
$errorMsg = "Mysql Error in preheader.php q(). The query was: " . $q . " and the possible mysqli error follows:" . $mysqliConn->error;
//logError($errorMsg);
exit("<p>$errorMsg</p>");
}
}
else{
//mysql connection; was replaced in v8.6 with mysqli
$r = mysql_query($q);
if(mysql_error()){
echo mysql_error();
echo "$q<br>";
}
}
if($debug == 1){
echo "<br>$q<br>";
}
if(stristr(substr($q,0,8),"delete") || stristr(substr($q,0,8),"insert") || stristr(substr($q,0,8),"update")){
if ($useMySQLi){
$affectedRows = $mysqliConn->affected_rows;
}
else{
$affectedRows = mysql_affected_rows();
}
if ($affectedRows > 0){
return true;
}
return false;
}
if ($useMySQLi){
$numRows = $r->num_rows;
}
else{
$numRows = mysql_num_rows($r);
}
if ($numRows > 1){
if ($useMySQLi){
while ($row = $r->fetch_array()){
$results[] = $row;
}
}
else{
while($row = mysql_fetch_array($r)){
$results[] = $row;
}
}
}
else if ($numRows == 1){
$results = array();
if ($useMySQLi){
$results[] = $r->fetch_array();
}
else{
$results[] = mysql_fetch_array($r);
}
}
else{
$results = array();
}
return $results;
}
}
if (!function_exists('q1')) {
function q1($q, $debug = 0){
global $mysqliConn, $useMySQLi;
if ($useMySQLi){
if (!($r = $mysqliConn->query($q))) {
$errorMsg = "Mysql Error in preheader.php q1(). The query was: " . $q . " and the possible mysqli error follows:" . $mysqliConn->error;
//logError($errorMsg);
exit($errorMsg);
}
}
else{
$r = mysql_query($q);
if(mysql_error()){
echo mysql_error();
echo "<br>$q<br>";
}
}
if($debug == 1){
echo "<br>$q<br>";
}
if ($useMySQLi && isset($r)){
$row = $r->fetch_array();
}
else{
$row = @mysql_fetch_array($r);
}
if(count($row) == 2){
return $row[0];
}
return $row;
}
}
if (!function_exists('qr')) {
function qr($q, $debug = 0){
global $mysqliConn, $useMySQLi;
if ($useMySQLi){
if (!($r = $mysqliConn->query($q))) {
$errorMsg = "Mysql Error in preheader.php qr(). The query was: " . $q . " and the possible mysqli error follows:" . $mysqliConn->error;
exit("<p>$errorMsg</p>");
}
}
else{
$r = mysql_query($q);
if(mysql_error()){
echo mysql_error();
echo "<br>$q<br>";
}
}
if($debug == 1){
echo "<br>$q<br>";
}
if(stristr(substr($q,0,8),"delete") || stristr(substr($q,0,8),"insert") || stristr(substr($q,0,8),"update")){
if ($useMySQLi){
$numberOfAffectedRows = $mysqliConn->affected_rows;
}
else{
$numberOfAffectedRows = mysql_affected_rows();
}
if ($numberOfAffectedRows > 0) {
return true;
}
return false;
}
if(stristr(substr($q,0,8),"create") || stristr(substr($q,0,8),"drop")){
//added for executing create table statements; e.g. the example install script /examples/install.php
return true;
}
$results = array();
if ($useMySQLi){
$results[] = $r->fetch_array();
}
else{
$results[] = mysql_fetch_array($r);
}
$results = $results[0];
return $results;
}
}
?>