-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasiermysql.php
271 lines (227 loc) · 6.74 KB
/
easiermysql.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
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
<?php
/*
easiermysql.php - Helper functions to make MySQL / MariaDB access smoother.
These functions enable you to insert and update MySQL content using regular
PHP arrays. Prefix your array key with @ if you want to set raw values,
such as when using MySQL functions.
Updated to use mysqli and work with PHP7.
Written by Johannes Ridderstedt ([email protected])
Released to the public domain.
Last update 2023-08-23.
*/
/*=============================================================
Build an INSERT query & run it. No more manual INSERTs.
Also makes your code more safe with mysqli_escape_string.
Example:
$insert = array(
'username' => $username,
'password' => $password,
'@created' => 'now()'
);
$userid = mInsert ("user", $insert);
===============================================================*/
function mInsert ($table, $data) {
global $mysql_link;
$names = '';
$values = '';
foreach ($data as $key => $value) {
if (substr($key,0,1) == '@') {
$names .= substr($key,1).',';
$values .= $value.',';
} else {
$names .= $key.',';
$values .= '"'.mysqli_escape_string($mysql_link,$value).'",';
}
}
// INSERT INTO table (a,b,c) values (1,2,3)
$query = 'INSERT INTO '.$table.' '.
'('.substr($names ,0,strlen($names )-1).') values '.
'('.substr($values,0,strlen($values)-1).')';
mysqli_query($mysql_link,$query);
return mysqli_insert_id($mysql_link);
}
/*=============================================================
Build an UPDATE query for you.
Example:
$data = array(
'id' => $_POST[id],
'username' => $_SESSION[username],
'password' => $_POST[newpassword],
'@updated' => 'now()'
);
$query = mUpdate ("user", "id", $data);
===============================================================*/
function mUpdate ($table, $primary, $data) {
global $mysql_link;
/* Support both single and combined primary keys. */
if(gettype($primary)=='string'){
$primary=array($primary);
}
$where = '';
foreach ($primary as $k) {
$where .= $k.'="'.$data[$k].'" AND ';
unset($data[$k]);
}
$set = '';
foreach ($data as $key => $value) {
if (substr($key,0,1) == '@')
$set .= substr($key,1).'='.$value.',';
else if (is_null($value))
$set .= $key.'=NULL,';
else
$set .= $key.'="'.mysqli_escape_string($mysql_link,$value).'",';
}
// UPDATE table SET a=1, b=2, c=3
$query = 'UPDATE '.$table.' SET '.substr($set,0,strlen($set)-1).
' WHERE '.substr($where,0,strlen($where)-5);
$res = mysqli_query($mysql_link,$query);
return mysqli_affected_rows($mysql_link);
}
/*=============================================================
Return an iterator for all rows in a MySQL result.
*/
function res2iterator($res) {
if(!$res) return array();
return new mIterator($res);
}
class mIterator implements Iterator {
public function __construct($res) {
$this->res = $res;
}
public function valid () {
if ($this->row) return true;
return false;
}
public function current () {
return $this->row;
}
public function key () {
return $this->key;
}
public function next () {
$this->row = mysqli_fetch_assoc($this->res);
$this->key++;
}
public function rewind () {
$this->key = -1;
$this->next();
}
}
/*=============================================================
Return an array of all rows in a MySQL result.
*/
function res2array($res) {
if(!$res || $res->num_rows == 0)
return array();
while($row=mysqli_fetch_assoc($res)){
$array[] = $row;
}
return $array;
}
/*=============================================================
Return a keyed array of all rows in a MySQL result.
*/
function res2keyarray($res,$key) {
if(!$res || $res->num_rows == 0)
return array();
while($row=mysqli_fetch_assoc($res)){
$array[$row[$key]] = $row;
}
return $array;
}
function mConnect() {
global $mysql_link;
if (isset($GLOBALS['mysql_port']))
$mysql_link = mysqli_connect(
$GLOBALS['mysql_hostname'],
$GLOBALS['mysql_username'],
$GLOBALS['mysql_password'],
$GLOBALS['mysql_database'],
$GLOBALS['mysql_port']
);
else
$mysql_link = mysqli_connect(
$GLOBALS['mysql_hostname'],
$GLOBALS['mysql_username'],
$GLOBALS['mysql_password'],
$GLOBALS['mysql_database']
);
if ($mysql_link)
mysqli_set_charset($mysql_link,'utf8mb4');
return $mysql_link;
}
function mClose() {
global $mysql_link;
mysqli_close($mysql_link);
}
/*
Use this to easy and safely select rows from the database.
Put @ for all variables you need in the query string, and then pass a list
of all variables as the second argument. These values will be integrated
into the query string using mysqli_escape_string.
mSelectRows( 'SELECT * FROM users WHERE name = @', array('John Doe') );
*/
function mSelectRows ($q, $escapelist = null) {
global $mysql_link;
$res = mysqli_query( $mysql_link, mEscape($q,$escapelist) );
if (!$res) return null;
return res2array($res);
}
/*
Same as mSelectRows but uses an iterator instead (for huge result sets).
*/
function mSelectManyRows ($q, $escapelist = null) {
global $mysql_link;
$res = mysqli_query( $mysql_link, mEscape($q,$escapelist) );
if (!$res) return null;
return res2iterator($res);
}
/*
Select a single row.
*/
function mSelectOne ($q, $escapelist = null) {
global $mysql_link;
$res = mysqli_query( $mysql_link, mEscape($q,$escapelist) );
if (!$res || $res->num_rows <= 0) return null;
return mysqli_fetch_assoc($res);
}
/*
Generic query with @-escaping support.
*/
function mQuery ($q, $escapelist = null) {
global $mysql_link;
return mysqli_query( $mysql_link, mEscape($q,$escapelist) );
}
/*
Delete with @-escaping and showing how many rows that were deleted.
*/
function mDelete ($q, $escapelist = null) {
global $mysql_link;
$res = mysqli_query( $mysql_link, mEscape($q,$escapelist) );
return mysqli_affected_rows($mysql_link);
}
function mEscapeValue ($s) {
global $mysql_link;
return '"'. mysqli_escape_string( $mysql_link, $s ) .'"';
}
function mEscape ($q, $escapelist = null) {
global $mysql_link;
while ($i = strpos($q,'@')) {
$parts[] = substr($q,0,$i);
$q = substr($q,$i+1);
$parts[] = '"'. mysqli_escape_string( $mysql_link, array_shift($escapelist) ) .'"';
}
$parts[] = $q;
return implode('',$parts);
}
function mError () {
global $mysql_link;
return array( mysqli_errno($mysql_link), mysqli_error($mysql_link) );
}
/* Don't use the PHP 8.1+ default of exceptions for MySQL errors. */
mysqli_report( MYSQLI_REPORT_OFF );
/*
A simple yet fully real-world usable and battle tested MySQL API. If you
need different behavior or extra features, change the code to your needs.
*/
?>