FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PHP-MySQL-Class/class.MySQL.php at master · a1phanumeric/PHP-MySQL-Class · GitHub
a1phanumeric
/
PHP-MySQL-Class
Public
Notifications
You must be signed in to change notification settings
Fork
248
Star
360
Code
Issues
16
Pull requests
4
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
PHP-MySQL-Class
/
class.MySQL.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
415 lines (336 loc) · 11.9 KB
Breadcrumbs
PHP-MySQL-Class
/
class.MySQL.php
Copy path
File metadata and controls
415 lines (336 loc) · 11.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/*
* Copyright (C) 2012
* Ed Rackham (http://github.com/a1phanumeric/PHP-MySQL-Class)
* Changes to Version 0.8.1 copyright (C) 2013
* Christopher Harms (http://github.com/neurotroph)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// MySQL Class v0.8.1
class
MySQL {
// Base variables
public
$
lastError
;
// Holds the last error
public
$
lastQuery
;
// Holds the last query
public
$
result
;
// Holds the MySQL query result
public
$
records
;
// Holds the total number of records returned
public
$
affected
;
// Holds the total number of records affected
public
$
rawResults
;
// Holds raw 'arrayed' results
public
$
arrayedResult
;
// Holds an array of the result
private
$
hostname
;
// MySQL Hostname
private
$
username
;
// MySQL Username
private
$
password
;
// MySQL Password
private
$
database
;
// MySQL Database
private
$
databaseLink
;
// Database Connection Link
/* *******************
* Class Constructor *
* *******************/
function
__construct
(
$
database
,
$
username
,
$
password
,
$
hostname
=
'
localhost
'
,
$
port
=
3306
,
$
persistant
=
false
){
$
this
->
database
=
$
database
;
$
this
->
username
=
$
username
;
$
this
->
password
=
$
password
;
$
this
->
hostname
=
$
hostname
.
'
:
'
.
$
port
;
$
this
->
Connect
(
$
persistant
);
}
/* *******************
* Class Destructor *
* *******************/
function
__destruct
(){
$
this
->
closeConnection
();
}
/* *******************
* Private Functions *
* *******************/
// Connects class to database
// $persistant (boolean) - Use persistant connection?
private
function
Connect
(
$
persistant
=
false
){
$
this
->
CloseConnection
();
if
(
$
persistant
){
$
this
->
databaseLink
=
mysql_pconnect
(
$
this
->
hostname
,
$
this
->
username
,
$
this
->
password
);
}
else
{
$
this
->
databaseLink
=
mysql_connect
(
$
this
->
hostname
,
$
this
->
username
,
$
this
->
password
);
}
if
(!
$
this
->
databaseLink
){
$
this
->
lastError
=
'
Could not connect to server:
'
.
mysql_error
(
$
this
->
databaseLink
);
return
false
;
}
if
(!
$
this
->
UseDB
()){
$
this
->
lastError
=
'
Could not connect to database:
'
.
mysql_error
(
$
this
->
databaseLink
);
return
false
;
}
$
this
->
setCharset
();
// TODO: remove forced charset find out a specific management
return
true
;
}
// Select database to use
private
function
UseDB
(){
if
(!
mysql_select_db
(
$
this
->
database
,
$
this
->
databaseLink
)){
$
this
->
lastError
=
'
Cannot select database:
'
.
mysql_error
(
$
this
->
databaseLink
);
return
false
;
}
else
{
return
true
;
}
}
// Performs a 'mysql_real_escape_string' on the entire array/string
private
function
SecureData
(
$
data
,
$
types
=
array
()){
if
(
is_array
(
$
data
)){
$
i
=
0
;
foreach
(
$
data
as
$
key
=>
$
val
){
if
(!
is_array
(
$
data
[
$
key
])){
$
data
[
$
key
] =
$
this
->
CleanData
(
$
data
[
$
key
],
$
types
[
$
i
]);
$
data
[
$
key
] =
mysql_real_escape_string
(
$
data
[
$
key
],
$
this
->
databaseLink
);
$
i
++;
}
}
}
else
{
$
data
=
$
this
->
CleanData
(
$
data
,
$
types
);
$
data
=
mysql_real_escape_string
(
$
data
,
$
this
->
databaseLink
);
}
return
$
data
;
}
// clean the variable with given types
// possible types: none, str, int, float, bool, datetime, ts2dt (given timestamp convert to mysql datetime)
// bonus types: hexcolor, email
private
function
CleanData
(
$
data
,
$
type
=
''
){
switch
(
$
type
) {
case
'
none
'
:
// useless do not reaffect just do nothing
//$data = $data;
break
;
case
'
str
'
:
case
'
string
'
:
settype
(
$
data
,
'
string
'
);
break
;
case
'
int
'
:
case
'
integer
'
:
settype
(
$
data
,
'
integer
'
);
break
;
case
'
float
'
:
settype
(
$
data
,
'
float
'
);
break
;
case
'
bool
'
:
case
'
boolean
'
:
settype
(
$
data
,
'
boolean
'
);
break
;
// Y-m-d H:i:s
// 2014-01-01 12:30:30
case
'
datetime
'
:
$
data
=
trim
(
$
data
);
$
data
=
preg_replace
(
'
/[^\d\-: ]/i
'
,
''
,
$
data
);
preg_match
(
'
/^([\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2})$/
'
,
$
data
,
$
matches
);
$
data
=
$
matches
[
1
];
break
;
case
'
ts2dt
'
:
settype
(
$
data
,
'
integer
'
);
$
data
=
date
(
'
Y-m-d H:i:s
'
,
$
data
);
break
;
// bonus types
case
'
hexcolor
'
:
preg_match
(
'
/(#[0-9abcdef]{6})/i
'
,
$
data
,
$
matches
);
$
data
=
$
matches
[
1
];
break
;
case
'
email
'
:
$
data
=
filter_var
(
$
data
,
FILTER_VALIDATE_EMAIL
);
break
;
default
:
break
;
}
return
$
data
;
}
/* ******************
* Public Functions *
* ******************/
// Executes MySQL query
public
function
executeSQL
(
$
query
){
$
this
->
lastQuery
=
$
query
;
if
(
$
this
->
result
=
mysql_query
(
$
query
,
$
this
->
databaseLink
)){
if
(
gettype
(
$
this
->
result
) ===
'
resource
'
) {
$
this
->
records
= @
mysql_num_rows
(
$
this
->
result
);
}
else
{
$
this
->
records
=
0
;
}
$
this
->
affected
= @
mysql_affected_rows
(
$
this
->
databaseLink
);
if
(
$
this
->
records
>
0
){
$
this
->
arrayResults
();
return
$
this
->
arrayedResult
;
}
else
{
return
true
;
}
}
else
{
$
this
->
lastError
=
mysql_error
(
$
this
->
databaseLink
);
return
false
;
}
}
public
function
commit
(){
return
mysql_query
(
"
COMMIT
"
,
$
this
->
databaseLink
);
}
public
function
rollback
(){
return
mysql_query
(
"
ROLLBACK
"
,
$
this
->
databaseLink
);
}
public
function
setCharset
(
$
charset
=
'
UTF8
'
) {
return
mysql_set_charset
(
$
this
->
SecureData
(
$
charset
,
'
string
'
),
$
this
->
databaseLink
);
}
// Adds a record to the database based on the array key names
public
function
insert
(
$
table
,
$
vars
,
$
exclude
=
''
,
$
datatypes
=
array
()){
// Catch Exclusions
if
(
$
exclude
==
''
){
$
exclude
=
array
();
}
array_push
(
$
exclude
,
'
MAX_FILE_SIZE
'
);
// Automatically exclude this one
// Prepare Variables
$
vars
=
$
this
->
SecureData
(
$
vars
,
$
datatypes
);
$
query
=
"
INSERT INTO `
{
$
table
}
` SET
"
;
foreach
(
$
vars
as
$
key
=>
$
value
){
if
(
in_array
(
$
key
,
$
exclude
)){
continue
;
}
$
query
.=
"
`
{
$
key
}
` = '
{
$
value
}
',
"
;
}
$
query
=
trim
(
$
query
,
'
,
'
);
return
$
this
->
executeSQL
(
$
query
);
}
// Deletes a record from the database
public
function
delete
(
$
table
,
$
where
=
''
,
$
limit
=
''
,
$
like
=
false
,
$
wheretypes
=
array
()){
$
query
=
"
DELETE FROM `
{
$
table
}
` WHERE
"
;
if
(
is_array
(
$
where
) &&
$
where
!=
''
){
// Prepare Variables
$
where
=
$
this
->
SecureData
(
$
where
,
$
wheretypes
);
foreach
(
$
where
as
$
key
=>
$
value
){
if
(
$
like
){
$
query
.=
"
`
{
$
key
}
` LIKE '%
{
$
value
}
%' AND
"
;
}
else
{
$
query
.=
"
`
{
$
key
}
` = '
{
$
value
}
' AND
"
;
}
}
$
query
=
substr
(
$
query
,
0
, -
5
);
}
if
(
$
limit
!=
''
){
$
query
.=
'
LIMIT
'
.
$
limit
;
}
$
result
=
$
this
->
executeSQL
(
$
query
);
if
(
$
this
->
affected
==
0
){
return
false
;
}
return
$
result
;
}
// Gets a single row from $from where $where is true
public
function
select
(
$
from
,
$
where
=
''
,
$
orderBy
=
''
,
$
limit
=
''
,
$
like
=
false
,
$
operand
=
'
AND
'
,
$
cols
=
'
*
'
,
$
wheretypes
=
array
()){
// Catch Exceptions
if
(
trim
(
$
from
) ==
''
){
return
false
;
}
$
query
=
"
SELECT
{
$
cols
}
FROM `
{
$
from
}
` WHERE
"
;
if
(
is_array
(
$
where
) &&
$
where
!=
''
){
// Prepare Variables
$
where
=
$
this
->
SecureData
(
$
where
,
$
wheretypes
);
foreach
(
$
where
as
$
key
=>
$
value
){
if
(
$
like
){
$
query
.=
"
`
{
$
key
}
` LIKE '%
{
$
value
}
%'
{
$
operand
}
"
;
}
else
{
$
query
.=
"
`
{
$
key
}
` = '
{
$
value
}
'
{
$
operand
}
"
;
}
}
$
query
=
substr
(
$
query
,
0
, -(
strlen
(
$
operand
)+
2
));
}
else
{
$
query
=
substr
(
$
query
,
0
, -
6
);
}
if
(
$
orderBy
!=
''
){
$
query
.=
'
ORDER BY
'
.
$
orderBy
;
}
if
(
$
limit
!=
''
){
$
query
.=
'
LIMIT
'
.
$
limit
;
}
$
result
=
$
this
->
executeSQL
(
$
query
);
if
(
is_array
(
$
result
))
return
$
result
;
return
array
();
}
// Updates a record in the database based on WHERE
public
function
update
(
$
table
,
$
set
,
$
where
,
$
exclude
=
''
,
$
datatypes
=
array
(),
$
wheretypes
=
array
()){
// Catch Exceptions
if
(
trim
(
$
table
) ==
''
|| !
is_array
(
$
set
) || !
is_array
(
$
where
)){
return
false
;
}
if
(
$
exclude
==
''
){
$
exclude
=
array
();
}
array_push
(
$
exclude
,
'
MAX_FILE_SIZE
'
);
// Automatically exclude this one
$
set
=
$
this
->
SecureData
(
$
set
,
$
datatypes
);
$
where
=
$
this
->
SecureData
(
$
where
,
$
wheretypes
);
// SET
$
query
=
"
UPDATE `
{
$
table
}
` SET
"
;
foreach
(
$
set
as
$
key
=>
$
value
){
if
(
in_array
(
$
key
,
$
exclude
)){
continue
;
}
$
query
.=
"
`
{
$
key
}
` = '
{
$
value
}
',
"
;
}
$
query
=
substr
(
$
query
,
0
, -
2
);
// WHERE
$
query
.=
'
WHERE
'
;
foreach
(
$
where
as
$
key
=>
$
value
){
$
query
.=
"
`
{
$
key
}
` = '
{
$
value
}
' AND
"
;
}
$
query
=
substr
(
$
query
,
0
, -
5
);
$
result
=
$
this
->
executeSQL
(
$
query
);
if
(
$
this
->
affected
==
0
){
return
false
;
}
return
$
result
;
}
// 'Arrays' a single result
public
function
arrayResult
(){
$
this
->
arrayedResult
=
mysql_fetch_assoc
(
$
this
->
result
)
or
die
(
mysql_error
(
$
this
->
databaseLink
));
return
$
this
->
arrayedResult
;
}
// 'Arrays' multiple result
public
function
arrayResults
(){
if
(
$
this
->
records
==
1
){
return
$
this
->
arrayResult
();
}
$
this
->
arrayedResult
=
array
();
while
(
$
data
=
mysql_fetch_assoc
(
$
this
->
result
)){
$
this
->
arrayedResult
[] =
$
data
;
}
return
$
this
->
arrayedResult
;
}
// 'Arrays' multiple results with a key
public
function
arrayResultsWithKey
(
$
key
=
'
id
'
){
if
(
isset
(
$
this
->
arrayedResult
)){
unset(
$
this
->
arrayedResult
);
}
$
this
->
arrayedResult
=
array
();
while
(
$
row
=
mysql_fetch_assoc
(
$
this
->
result
)){
foreach
(
$
row
as
$
theKey
=>
$
theValue
){
$
this
->
arrayedResult
[
$
row
[
$
key
]][
$
theKey
] =
$
theValue
;
}
}
return
$
this
->
arrayedResult
;
}
// Returns last insert ID
public
function
lastInsertID
(){
return
mysql_insert_id
(
$
this
->
databaseLink
);
}
// Return number of rows
public
function
countRows
(
$
from
,
$
where
=
''
){
$
result
=
$
this
->
select
(
$
from
,
$
where
,
''
,
''
,
false
,
'
AND
'
,
'
count(*)
'
);
return
$
result
[
"
count(*)
"
];
}
// Closes the connections
public
function
closeConnection
(){
if
(
$
this
->
databaseLink
){
// Commit before closing just in case :)
$
this
->
commit
();
mysql_close
(
$
this
->
databaseLink
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL