5
* @version $Id: mysqli.php,v 1.36 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
19
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22
* MySQLi Database Abstraction Layer
23
* mysqli-extension has to be compiled with:
24
* MySQL 4.1+ or MySQL 5.0+
27
class dbal_mysqli extends dbal
29
var $multi_insert = true;
34
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
36
$this->persistency = $persistency;
37
$this->user = $sqluser;
38
$this->server = $sqlserver;
39
$this->dbname = $database;
40
$port = (!$port) ? NULL : $port;
42
// Persistant connections not supported by the mysqli extension?
43
$this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
45
if ($this->db_connect_id && $this->dbname != '')
47
@mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
48
// enforce strict mode on databases that support it
49
if (mysqli_get_server_version($this->db_connect_id) >= 50002)
51
$result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
52
$row = @mysqli_fetch_assoc($result);
53
@mysqli_free_result($result);
54
$modes = array_map('trim', explode(',', $row['sql_mode']));
56
// TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
57
if (!in_array('TRADITIONAL', $modes))
59
if (!in_array('STRICT_ALL_TABLES', $modes))
61
$modes[] = 'STRICT_ALL_TABLES';
64
if (!in_array('STRICT_TRANS_TABLES', $modes))
66
$modes[] = 'STRICT_TRANS_TABLES';
70
$mode = implode(',', $modes);
71
@mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
73
return $this->db_connect_id;
76
return $this->sql_error('');
80
* Version information about used database
82
function sql_server_info()
84
return 'MySQL(i) ' . @mysqli_get_server_info($this->db_connect_id);
91
function _sql_transaction($status = 'begin')
96
return @mysqli_autocommit($this->db_connect_id, false);
100
$result = @mysqli_commit($this->db_connect_id);
101
@mysqli_autocommit($this->db_connect_id, true);
106
$result = @mysqli_rollback($this->db_connect_id);
107
@mysqli_autocommit($this->db_connect_id, true);
118
* @param string $query Contains the SQL query which shall be executed
119
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
120
* @return mixed When casted to bool the returned value returns true on success and false on failure
124
function sql_query($query = '', $cache_ttl = 0)
130
// EXPLAIN only in extra debug mode
131
if (defined('DEBUG_EXTRA'))
133
$this->sql_report('start', $query);
136
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
137
$this->sql_add_num_queries($this->query_result);
139
if ($this->query_result === false)
141
if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
143
$this->sql_error($query);
146
if (defined('DEBUG_EXTRA'))
148
$this->sql_report('stop', $query);
151
if ($cache_ttl && method_exists($cache, 'sql_save'))
153
$cache->sql_save($query, $this->query_result, $cache_ttl);
156
else if (defined('DEBUG_EXTRA'))
158
$this->sql_report('fromcache', $query);
166
return ($this->query_result) ? $this->query_result : false;
172
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
174
$this->query_result = false;
176
// if $total is set to 0 we do not want to limit the number of rows
179
// MySQL 4.1+ no longer supports -1 in limit queries
180
$total = '18446744073709551615';
183
$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
185
return $this->sql_query($query, $cache_ttl);
189
* Return number of affected rows
191
function sql_affectedrows()
193
return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
199
function sql_fetchrow($query_id = false)
203
if ($query_id === false)
205
$query_id = $this->query_result;
208
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
210
return $cache->sql_fetchrow($query_id);
213
return ($query_id !== false) ? @mysqli_fetch_assoc($query_id) : false;
217
* Seek to given row number
218
* rownum is zero-based
220
function sql_rowseek($rownum, &$query_id)
224
if ($query_id === false)
226
$query_id = $this->query_result;
229
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
231
return $cache->sql_rowseek($rownum, $query_id);
234
return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
238
* Get last inserted id after insert statement
240
function sql_nextid()
242
return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
248
function sql_freeresult($query_id = false)
252
if ($query_id === false)
254
$query_id = $this->query_result;
257
if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
259
return $cache->sql_freeresult($query_id);
262
return @mysqli_free_result($query_id);
266
* Escape string used in sql query
268
function sql_escape($msg)
270
return @mysqli_real_escape_string($this->db_connect_id, $msg);
274
* Build LIKE expression
277
function _sql_like_expression($expression)
283
* Build db-specific query data
286
function _sql_custom_build($stage, $data)
291
$data = '(' . $data . ')';
299
* return sql error array
302
function _sql_error()
304
if (!$this->db_connect_id)
307
'message' => @mysqli_connect_error(),
308
'code' => @mysqli_connect_errno()
313
'message' => @mysqli_error($this->db_connect_id),
314
'code' => @mysqli_errno($this->db_connect_id)
319
* Close sql connection
322
function _sql_close()
324
return @mysqli_close($this->db_connect_id);
328
* Build db-specific report
331
function _sql_report($mode, $query = '')
335
// current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
336
if ($test_prof === null)
339
if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
341
$ver = mysqli_get_server_version($this->db_connect_id);
342
if ($ver >= 50037 && $ver < 50100)
353
$explain_query = $query;
354
if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
356
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
358
else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
360
$explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
363
if (preg_match('/^SELECT/', $explain_query))
370
@mysqli_query($this->db_connect_id, 'SET profiling = 1;');
373
if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
375
while ($row = @mysqli_fetch_assoc($result))
377
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
380
@mysqli_free_result($result);
384
$this->html_hold .= '</table>';
391
// get the last profile
392
if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
394
$this->html_hold .= '<br />';
395
while ($row = @mysqli_fetch_assoc($result))
397
// make <unknown> HTML safe
398
if (!empty($row['Source_function']))
400
$row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
403
// remove unsupported features
404
foreach ($row as $key => $val)
411
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
414
@mysqli_free_result($result);
418
$this->html_hold .= '</table>';
421
@mysqli_query($this->db_connect_id, 'SET profiling = 0;');
428
$endtime = explode(' ', microtime());
429
$endtime = $endtime[0] + $endtime[1];
431
$result = @mysqli_query($this->db_connect_id, $query);
432
while ($void = @mysqli_fetch_assoc($result))
434
// Take the time spent on parsing rows into account
436
@mysqli_free_result($result);
438
$splittime = explode(' ', microtime());
439
$splittime = $splittime[0] + $splittime[1];
441
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'