-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesqli.php
195 lines (165 loc) · 5.52 KB
/
esqli.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
<?php
include ("config.php");
if( !class_exists("esql") ) {
/*
linebreaks for sync with easysql.php
*/
class esql{
//easySQLi (eSQLi) Mini-API
//flag to set status of eSQLi to enabled.
public static $esqlienabled=true;
//array for keeping connections
private static $db=array();
//connect and select db
public static function dbc($link,$db=NULL,$user=NULL,$pass=NULL,$host=NULL,$debug=false){ //connect
if($db===NULL) {
$db=config::$dbname;
}
if($user===NULL) {
$user=config::$dbuser;
}
if($host===NULL) {
$host=config::$dbhost;
}
if($pass===NULL) {
$pass=config::$dbpass;
}
self::$db[$link]=mysqli_connect($host,$user,$pass) or $error=style::error(lang::$sqlerror."(".$link.")") and die ($error); //new link not needed because already enforced
mysqli_select_db(self::$db[$link],$db) or $error=style::error('cannot select db '.$db.' on link '.$link) and die ($error);
mysqli_set_charset(self::$db[$link],'utf8');
if($debug) {
echo "opened ".$link."<br>";
}
}
//query
public static function dbq($link,$action, $col/*leave empty for delete, set param for update,cols für insert*/,
$table,$filter=""/*filter includes sort and group nofilter is 1 -> values bei insert*/,
$debug=false){
$q="";
if($col != "*"&&$action!="update") {
$cols=explode(",",$col);
foreach($cols as &$c) {
if(preg_match('/^(count|min|max)\(.+\)$/',$c)){
if($debug) echo "function escape";
$c=preg_replace('/(count|min|max)\(([^*]+)\)/','$1(`$2`)',$c);
}
else {
if(preg_match('/^[A-Za-z_$]+$/',$c)) {
$c=trim($c,"`");
$c="`".$c."`";
}
}
}
$col=implode(",",$cols);
}
//trim, prefix and re-escape
$table=str_replace("`","",$table);
$tbls=explode(",",$table);
foreach($tbls as &$t) {
$t=config::$prefix.$t;
$t="`".$t."`";
}
$table=implode(",",$tbls);
//arrange SQL command based on action
if($action=="select"){
if($filter)
$q="select ".$col." from ".$table." where ".$filter;
else
$q="select ".$col." from ".$table;
}
if($action=="update"&&$filter)
$q="update ".$table." set ".$col." where ".$filter;
if($action=="delete"&&$filter)
$q="delete from ".$table." where ".$filter;
if($action=="insert")
$q='insert into '.$table.' '.'('.$col.') values ('.$filter.')';
if($debug)
echo $q."<br>"; // -> debug
if($q) {
$q=mysqli_query(self::$db[$link],$q);
if($q===false) { //if query result is empty
if($debug) {
echo mysqli_error(self::$db[$link]);
echo "<br>";
var_dump(self::$db);
echo "<br>";
die ("cannot ".$action." in db (".$link.")");
}
else{
echo(style::w("Datenbakfehler. ".$action." in handle ".$link));
}
}
}
return $q;
}
//num rows
public static function num($res) {
return mysqli_num_rows($res);
}
//affected rows (edit/update/insert)
public static function affrows($link){
return mysqli_affected_rows (self::$db[$link]);
}
//mysql(i)_info parser for update
public static function updinfo($link,$data='') {
$list=mysqli_info (self::$db[$link]);
$list=str_replace('Rows matched','match',$list);
preg_match_all ('/(\S[^:]+): (\d+)/', $list, $matches);
$info = array_combine ($matches[1], $matches[2]);
var_dump($info);
if(!$data) {
return $info;
}
elseif($data="match") {
return $info["match"];
}
}
//fetch row
public static function frow($res) {
return mysqli_fetch_row($res);
}
//fetch one row as array
public static function farray($res,$style=MYSQLI_BOTH) {
return mysqli_fetch_array($res,$style);
}
//fetch all rows in one array
public static function fall($res,$style=MYSQLI_NUM) {
return mysqli_fetch_all ($res,$style);
}
//pinpoint result with row and column
public static function dbres($res,$row=0,$col=0) {
mysqli_data_seek($res,$row);
$datarow=mysqli_fetch_array($res);
return $datarow[$col];
}
//inserted ID
public static function iid($link) {
return mysqli_insert_id(self::$db[$link]);
}
//escape stuff
public static function escape($val,$con) {
if($con)
return mysqli_real_escape_string(self::$db[$con],$val);
else
return false;
}
//set seek pointer (e.g. for fetch row) to new position
public static function seek($res,$row) {
mysqli_data_seek($res,$row);
}
//close the connection
public static function dbclose($link="",$debug=false) {
if($link) {
if(mysqli_ping(self::$db[$link])) {
mysqli_close(self::$db[$link]);
if($debug) {
echo "closed ".$link."<br>";
}
}
}
}
}
}
//linebreaks to sync
//with easySQLn (normal mysql_* implementation)
?>