public
Description: PHP database abstraction class
Homepage: http://wiki.github.com/nshahzad/ezsql
Clone URL: git://github.com/nshahzad/ezdb.git
ezdb / ezDB_MySQLi.class.php
100644 355 lines (301 sloc) 8.58 kb
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
<?php
/**
* Codon PHP Framework
* www.nsslive.net/codon
* Software License Agreement (BSD License)
*
* Copyright (c) 2008 Nabeel Shahzad, nsslive.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nabeel Shahzad
* @copyright Copyright (c) 2008, Nabeel Shahzad
* @link http://www.nsslive.net/codon
* @license BSD License
* @package codon_core
*/
/**
* MySQLi implementation for ezDB
* By Nabeel Shahzad
*/
 
class ezDB_mysqli extends ezDB_Base
{
 
/**
* Constructor, connects to database immediately, unless $dbname is blank
*
* @param string $dbuser Database username
* @param string $dbpassword Database password
* @param string $dbname Database name (if blank, will not connect)
* @param string $dbhost Hostname, optional, default is 'localhost'
* @return bool Connect status
*
*/
public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
{
if($dbname == '') return false;
 
parent::__construct();
 
if($this->connect($dbuser, $dbpassword, $dbhost))
{
return $this->select($dbname);
}
 
return false;
}
 
/**
* Explicitly close the connection on destruct
*/
 
public function __destruct()
{
$this->close();
}
 
/**
* Connects to database immediately, unless $dbname is blank
*
* @param string $dbuser Database username
* @param string $dbpassword Database password
* @param string $dbname Database name (if blank, will not connect)
* @param string $dbhost Hostname, optional, default is 'localhost'
* @return bool Connect status
*
*/
public function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
{
$this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
}
 
/**
* Connect to MySQL, but not to a database
*
* @param string $dbuser Username
* @param string $dbpassword Password
* @param string $dbhost Host, optional, default is localhost
* @return bool Success
*
*/
public function connect($dbuser='', $dbpassword='', $dbhost='localhost')
{
$this->dbh = new mysqli($dbhost, $dbuser, $dbpassword);
 
if(mysqli_connect_errno() != 0)
{
if($this->use_exceptions)
throw new ezDB_Error(mysqli_connect_error(), mysqli_connect_errno());
 
$this->register_error(mysqli_connect_error(), mysqli_connect_errno());
return false;
}
else
{
$this->clear_errors();
return true;
}
 
return true;
}
 
/**
* Select a MySQL Database
*
* @param string $dbname Database name
* @return bool Success or not
*
*/
public function select($dbname='')
{
// Must have a database name
if ($dbname == '')
{
throw new ezDB_Error('No database name', -1);
$this->register_error('No database name specified!');
}
 
// Must have an active database connection
if(!$this->dbh)
{
if($this->use_exceptions)
throw new ezDB_Error(mysqli_connect_error(), mysqli_connect_errno());
 
$this->register_error('Can\'t select database, invalid or inactive connection', -1);
return false;
}
 
if(!$this->dbh->select_db($dbname))
{
if($this->use_exceptions)
throw new ezDB_Error($this->dbh->error, $this->dbh->errno);
 
$this->register_error($this->dbh->error, $this->dbh->errno);
return false;
}
else
{
$this->clear_errors();
return true;
}
 
return true;
}
 
/**
* Close the database connection
*/
public function close()
{
return @$this->dbh->close();
}
 
/**
* Format a mySQL string correctly for safe mySQL insert
* (no matter if magic quotes are on or not)
*
* @param string $str String to escape
* @return string Returns the escaped string
*
*/
public function escape($str)
{
return $this->dbh->real_escape_string($str);
}
 
/**
* Returns the DB specific timestamp function (Oracle: SYSDATE, MySQL: NOW())
*
* @return string Timestamp function
*
*/
public function sysdate()
{
return 'NOW()';
}
 
/**
* Run the SQL query, and get the result. Returns false on failure
* Check $this->error() and $this->errno() functions for any errors
* MySQL returns errno() == 0 for no error. That's the most reliable check
*
* @param string $query SQL Query
* @return mixed Return values
*
*/
public function query($query)
{
// Initialise return
$return_val = true;
 
// Flush cached values..
$this->flush();
 
// For reg expressions
$query = trim($query);
 
// Log how the function was called
$this->func_call = "\$db->query(\"$query\")";
 
// Keep track of the last query for debug..
$this->last_query = $query;
 
// Count how many queries there have been
$this->num_queries++;
 
// Use core file cache function
if($cache = $this->get_cache($query))
{
return $cache;
}
 
// If there is no existing database connection then try to connect
if ( ! $this->dbh )
{
if($this->use_exceptions)
throw new ezDB_Error($this->dbh->error, $this->dbh->errno);
 
$this->register_error('There is no active database connection!');
return false;
}
 
// Perform the query via std mysql_query function..
$result = $this->dbh->query($query);
 
if(is_bool($result))
{
if($result === false)
{
if($this->use_exceptions)
throw new ezDB_Error($this->dbh->error, $this->dbh->errno);
 
$this->register_error($this->dbh->error, $this->dbh->errno);
}
else
{
$this->clear_errors();
}
 
return $result;
}
 
// Query was an insert, delete, update, replace
$is_insert = false;
if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))
{
$this->rows_affected = $this->dbh->affected_rows;
$this->num_rows = $this->rows_affected;
 
if($this->dbh->insert_id > 0)
{
$this->insert_id = $this->dbh->insert_id;
$is_insert = true;
}
 
// Return number fo rows affected
$return_val = $this->rows_affected;
}
// Query was a select
else
{
// Take note of column info
$i=0;
 
if($result)
{
while ($finfo = $result->fetch_field())
{
$this->col_info[$i] = $finfo;
$i++;
}
 
// Store Query Results
$num_rows=0;
while($row = $result->fetch_object())
{
$this->last_result[$num_rows] = $row;
$num_rows++;
}
 
$result->close();
}
 
// Log number of rows the query returned
$this->rows_affected = $num_rows;
$this->num_rows = $num_rows;
 
// Return number of rows selected
$return_val = $this->num_rows;
}
 
// disk caching of queries
$this->store_cache($query,$is_insert);
 
// If debug ALL queries
$this->trace || $this->debug_all ? $this->debug() : null ;
 
return $return_val;
}
 
 
/* this is mysqli only
* incomplete implementation
*//*
function execute($query, $params)
{
if($this->mysql_version!=5 || $query == '' || $params == '')
return;
 
$stmt = $this->dbh->stmt_init();
if(!$stmt->prepare($query))
return false;
 
//bind our parameters
while(list($key, $value) = each($params))
{
if(is_double($value))
$type = 'd';
elseif(is_integer($value) || is_numeric($value))
$type = 'i';
else
$type = 's';
 
$stmt->bind_param($type, $value);
}
 
$stmt->execute();
 
}*/
}
 
?>