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
|
<?php
/**
* Minimal wrappers for core PHP mysql_* functions.
* @package mirror
* @subpackage lib
*/
/**
* Connect to a MySQL database server.
* @param string $host db server, defaults to localhost
* @param string $user db username
* @param string $password db password
* @return resource dbh
*/
function db_connect($host='localhost',$user=null,$password=null)
{
static $dbh = null;
if (!empty($host) && isset($user) && isset($password)) {
$dbh = @mysql_connect($host,$user,$password);
}
if (is_resource($dbh)) {
return $dbh;
}
else die("Unable to create database connection in db_connect()");
}
/**
* Select database.
* @param string $database name of the database to select
* @param resource $dbh valid dbh, null if not defined
* @return bool success of command
*/
function db_select($database,$dbh=null)
{
if(is_resource($dbh)){
return @mysql_select_db($database);
}else{
return @mysql_select_db($database, db_connect());
}
}
/**
* Execute a MySQL query.
* @param string $qry MySQL query
* @param resource $dbh valid dbh
*/
function db_query($qry=null,$dbh=null)
{
static $result = null;
if(!is_resource($dbh)) $dbh = db_connect();
if(is_null($qry))
{
if(is_resource($result)) return $result;
else return false;
}
else
{
$result = @mysql_query($qry,$dbh);
return $result;
}
}
/**
* Fetch a row as an array from a result.
* @param string $result (default to null)
* @return array
*/
function db_fetch($result=null,$type=MYSQL_BOTH)
{
return (!is_resource($result))? @mysql_fetch_array(db_query()) : @mysql_fetch_array($result,$type);
}
/**
* Fetch an array based on a query.
* @param string $query database query
* @param int $type result type
* @param string $col_id if passed it, the values of this column in the result set will be used as the array keys in the returned array
* @return array $list array of database rows
* Example of returned array:
* <code>
* db_get("SELECT * FROM table",MYSQL_ASSOC);
* returns...
* Array
* (
* [0] => Array
* (
* [id] => 1
* [field1] => data1
* [field2] => data2
* )
*
* )
* </code>
*/
function db_get($query,$type=MYSQL_BOTH,$col_id=NULL)
{
$res = db_query($query);
$list = array();
if (is_resource($res) && !is_null($col_id) && ($type == MYSQL_BOTH || $type == MYSQL_ASSOC) && @mysql_num_rows($res) !== 0) {
$col_test = db_fetch($res,$type);
@mysql_data_seek($res, 0);
if (array_key_exists($col_id,$col_test)) {
while ( $buf = db_fetch($res,$type) ) {
$list[$buf[$col_id]] = $buf;
}
return $list;
}
}
while ( $buf = db_fetch($res,$type) ) {
$list[] = $buf;
}
return $list;
}
/**
* Get all of the fieldnames for the specified table.
* @param string $table name of table to describe
* @return array array of column names, must be an array
*/
function db_fieldnames($table)
{
$dbh = db_connect();
$results = db_query("DESCRIBE $table");
if (is_resource($results))
{
while ($buf=db_fetch($results))
{
$field_names[] = $buf[0];
}
}
else
{
$field_names[] = 0;
}
return $field_names;
}
/**
* Create a MySQL INSERT statement based on $_POST array generated by form submission.
* <ul>
* <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li>
* <li>do not use clean_in() before this, or you'll have double the slashes</li>
* <li>use the function only when it saves you time, not _always_</li>
* <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li>
* </ul>
* @param array $vars array of posts
* @param string $table name of the table that fields will be inserted into
* @return string $query resulting MySQL insert string
*/
function db_makeinsert($vars,$table)
{
$dbh = db_connect();
$fields = db_fieldnames($table);
foreach ($fields as $field)
{
if (get_magic_quotes_gpc) $vars[$field] = stripslashes($vars[$field]);
$vars[$field] = addslashes($vars[$field]);
if (isset($vars[$field]))
{
isset($q1)?$q1 .= ','.$field:$q1='INSERT INTO '.$table.'('.$field;
isset($q2)?$q2 .= ",'$vars[$field]'":$q2=" VALUES('$vars[$field]'";
}
}
$q1 .= ')';
$q2 .= ')';
$query = $q1.$q2;
return $query;
}
/**
* Create a MySQL REPLACE statement based on $_POST array generated by form submission.
* <ul>
* <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li>
* <li>do not use clean_in() before this, or you'll have double the slashes</li>
* <li>use the function only when it saves you time, not _always_</li>
* <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li>
* </ul>
* @param array $vars array of posts
* @param string $table name of the table that fields will be inserted into
* @return string $query resulting MySQL insert string
*/
function db_makereplace($vars,$table)
{
$dbh = db_connect();
$fields = db_fieldnames($table);
foreach ($fields as $field)
{
if (get_magic_quotes_gpc) $vars[$field] = stripslashes($vars[$field]);
$vars[$field] = addslashes($vars[$field]);
if (isset($vars[$field]))
{
isset($q1)?$q1 .= ','.$field:$q1='REPLACE INTO '.$table.'('.$field;
isset($q2)?$q2 .= ",'$vars[$field]'":$q2=" VALUES('$vars[$field]'";
}
}
$q1 .= ')';
$q2 .= ')';
$query = $q1.$q2;
return $query;
}
/**
* Create a MySQL UPDATE statement based on $_POST array generated by form submission.
* <ul>
* <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li>
* <li>do not use clean_in() before this, or you'll have double the slashes</li>
* <li>use the function only when it saves you time, not _always_</li>
* <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li>
* </ul>
* @param array $vars array of posts
* @param string $table name of the table that fields will be inserted into
* @param string $where where clause, describing which records are to be updated
*/
function db_makeupdate($vars,$table,$where)
{
$dbh = db_connect();
$fields = db_fieldnames($table);
foreach ($fields as $field)
{
if (isset($vars[$field]))
{
if (get_magic_quotes_gpc()) $vars[$field] = stripslashes($vars[$field]);
$vars[$field]=addslashes($vars[$field]);
$q1 = isset($q1)?$q1 .= ' ,'.$field."='$vars[$field]'":'UPDATE '.$table.' set '.$field."='$vars[$field]'";
}
}
$query = $q1.' '.$where;
return $query;
}
/**
* Since PHP's mysql_insert_id() sometimes throws an error, this is the replacement
* @param resource $dbh optional dbh to get the last inserted id from
* @return int the return value of MySQL's last_insert_id()
*/
function db_insert_id($dbh=null)
{
if(!is_resource($dbh)) $dbh = db_connect();
$buf = db_fetch(db_query("SELECT LAST_INSERT_ID()", $dbh));
return empty($buf[0]) ? false : $buf[0];
}
/**
* Determine number of rows in result.
* @param resource $result mysql result
* @return int number of rows in query result
*/
function db_numrows($result=null)
{
return (!is_resource($result))? @mysql_num_rows(db_query()) : @mysql_num_rows($result);
}
/**
* Close the db connection. If a dbh is not specified, assume the last opened link.
* @param resource $dbh optional dbh to close
*/
function db_close($dbh=null)
{
return is_resource($dbh)?@mysql_close($dbh):@mysql_close();
}
/**
* Get one record.
* @param string $query query
* @param int $type result type
*/
function db_get_one($query,$type=MYSQL_ASSOC) {
$buf = db_get($query.' LIMIT 1',$type);
return $buf[0];
}
/**
* Get an ID based on name.
* @param string $table
* @param string $id_col
* @param string $name_col
* @param string $name
*/
function db_name_to_id($table,$id_col,$name_col,$name)
{
$buf = db_get_one("SELECT {$id_col} FROM {$table} WHERE {$name_col} = '{$name}'", MYSQL_NUM);
return $buf[0];
}
/**
* Sets enum booleans to their opposite
* @param string $table
* @param string $pri
* @param string $col
* @param array $id
* @return int
*/
function db_toggle_bool($table, $pri, $col, $id)
{
return db_query("UPDATE {$table} SET {$col} = IF({$col} = '1', '0', '1') WHERE {$pri} = {$id}");
}
?>
|