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 // Mysqli extension supports persistent connection since PHP 5.3.0
37 $this->persistency = (version_compare(PHP_VERSION, '5.3.0', '>=')) ? $persistency : false;
38 $this->user = $sqluser;
40 // If persistent connection, set dbhost to localhost when empty and prepend it with 'p:' prefix
41 $this->server = ($this->persistency) ? 'p:' . (($sqlserver) ? $sqlserver : 'localhost') : $sqlserver;
43 $this->dbname = $database;
44 $port = (!$port) ? NULL : $port;
46 // If port is set and it is not numeric, most likely mysqli socket is set.
47 // Try to map it to the $socket parameter.
51 if (is_numeric($port))
62 $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket);
64 if ($this->db_connect_id && $this->dbname != '')
66 @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
68 // enforce strict mode on databases that support it
69 if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
71 $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
72 $row = @mysqli_fetch_assoc($result);
73 @mysqli_free_result($result);
75 $modes = array_map('trim', explode(',', $row['sql_mode']));
77 // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
78 if (!in_array('TRADITIONAL', $modes))
80 if (!in_array('STRICT_ALL_TABLES', $modes))
82 $modes[] = 'STRICT_ALL_TABLES';
85 if (!in_array('STRICT_TRANS_TABLES', $modes))
87 $modes[] = 'STRICT_TRANS_TABLES';
91 $mode = implode(',', $modes);
92 @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
94 return $this->db_connect_id;
97 return $this->sql_error('');
101 * Version information about used database
102 * @param bool $use_cache If true, it is safe to retrieve the value from the cache
103 * @return string sql server version
105 function sql_server_info($raw = false, $use_cache = true)
109 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysqli_version')) === false)
111 $result = @mysqli_query($this->db_connect_id, 'SELECT VERSION() AS version');
112 $row = @mysqli_fetch_assoc($result);
113 @mysqli_free_result($result);
115 $this->sql_server_version = $row['version'];
117 if (!empty($cache) && $use_cache)
119 $cache->put('mysqli_version', $this->sql_server_version);
123 return ($raw) ? $this->sql_server_version : 'MySQL(i) ' . $this->sql_server_version;
130 function _sql_transaction($status = 'begin')
135 return @mysqli_autocommit($this->db_connect_id, false);
139 $result = @mysqli_commit($this->db_connect_id);
140 @mysqli_autocommit($this->db_connect_id, true);
145 $result = @mysqli_rollback($this->db_connect_id);
146 @mysqli_autocommit($this->db_connect_id, true);
157 * @param string $query Contains the SQL query which shall be executed
158 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
159 * @return mixed When casted to bool the returned value returns true on success and false on failure
163 function sql_query($query = '', $cache_ttl = 0)
169 // EXPLAIN only in extra debug mode
170 if (defined('DEBUG_EXTRA'))
172 $this->sql_report('start', $query);
175 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
176 $this->sql_add_num_queries($this->query_result);
178 if ($this->query_result === false)
180 if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
182 $this->sql_error($query);
185 if (defined('DEBUG_EXTRA'))
187 $this->sql_report('stop', $query);
190 if ($cache_ttl && method_exists($cache, 'sql_save'))
192 $cache->sql_save($query, $this->query_result, $cache_ttl);
195 else if (defined('DEBUG_EXTRA'))
197 $this->sql_report('fromcache', $query);
205 return $this->query_result;
211 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
213 $this->query_result = false;
215 // if $total is set to 0 we do not want to limit the number of rows
218 // MySQL 4.1+ no longer supports -1 in limit queries
219 $total = '18446744073709551615';
222 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
224 return $this->sql_query($query, $cache_ttl);
228 * Return number of affected rows
230 function sql_affectedrows()
232 return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
238 function sql_fetchrow($query_id = false)
242 if ($query_id === false)
244 $query_id = $this->query_result;
247 if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
249 return $cache->sql_fetchrow($query_id);
252 if ($query_id !== false)
254 $result = @mysqli_fetch_assoc($query_id);
255 return $result !== null ? $result : false;
262 * Seek to given row number
263 * rownum is zero-based
265 function sql_rowseek($rownum, &$query_id)
269 if ($query_id === false)
271 $query_id = $this->query_result;
274 if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
276 return $cache->sql_rowseek($rownum, $query_id);
279 return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
283 * Get last inserted id after insert statement
285 function sql_nextid()
287 return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
293 function sql_freeresult($query_id = false)
297 if ($query_id === false)
299 $query_id = $this->query_result;
302 if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
304 return $cache->sql_freeresult($query_id);
307 return @mysqli_free_result($query_id);
311 * Escape string used in sql query
313 function sql_escape($msg)
315 return @mysqli_real_escape_string($this->db_connect_id, $msg);
319 * Build LIKE expression
322 function _sql_like_expression($expression)
328 * Build db-specific query data
331 function _sql_custom_build($stage, $data)
336 $data = '(' . $data . ')';
344 * return sql error array
347 function _sql_error()
349 if (!$this->db_connect_id)
352 'message' => @mysqli_connect_error(),
353 'code' => @mysqli_connect_errno()
358 'message' => @mysqli_error($this->db_connect_id),
359 'code' => @mysqli_errno($this->db_connect_id)
364 * Close sql connection
367 function _sql_close()
369 return @mysqli_close($this->db_connect_id);
373 * Build db-specific report
376 function _sql_report($mode, $query = '')
380 // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
381 if ($test_prof === null)
384 if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
386 $ver = mysqli_get_server_version($this->db_connect_id);
387 if ($ver >= 50037 && $ver < 50100)
398 $explain_query = $query;
399 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
401 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
403 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
405 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
408 if (preg_match('/^SELECT/', $explain_query))
415 @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
418 if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
420 while ($row = @mysqli_fetch_assoc($result))
422 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
425 @mysqli_free_result($result);
429 $this->html_hold .= '</table>';
436 // get the last profile
437 if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
439 $this->html_hold .= '<br />';
440 while ($row = @mysqli_fetch_assoc($result))
442 // make <unknown> HTML safe
443 if (!empty($row['Source_function']))
445 $row['Source_function'] = str_replace(array('<', '>'), array('<', '>'), $row['Source_function']);
448 // remove unsupported features
449 foreach ($row as $key => $val)
456 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
459 @mysqli_free_result($result);
463 $this->html_hold .= '</table>';
466 @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
473 $endtime = explode(' ', microtime());
474 $endtime = $endtime[0] + $endtime[1];
476 $result = @mysqli_query($this->db_connect_id, $query);
477 while ($void = @mysqli_fetch_assoc($result))
479 // Take the time spent on parsing rows into account
481 @mysqli_free_result($result);
483 $splittime = explode(' ', microtime());
484 $splittime = $splittime[0] + $splittime[1];
486 $this->sql_report('record_fromcache', $query, $endtime, $splittime);