3 * This is the MySQLi database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Database abstraction object for PHP extension mysqli.
31 class DatabaseMysqli
extends DatabaseMysqlBase
{
32 /** @var $mConn mysqli */
38 protected function doQuery( $sql ) {
39 $conn = $this->getBindingHandle();
41 if ( $this->bufferResults() ) {
42 $ret = $conn->query( $sql );
44 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
51 * @param string $realServer
53 * @throws DBConnectionError
55 protected function mysqlConnect( $realServer ) {
56 # Avoid suppressed fatal error, which is very hard to track down
57 if ( !function_exists( 'mysqli_init' ) ) {
58 throw new DBConnectionError( $this, "MySQLi functions missing,"
59 . " have you compiled PHP with the --with-mysqli option?\n" );
62 // Other than mysql_connect, mysqli_real_connect expects an explicit port
63 // and socket parameters. So we need to parse the port and socket out of
67 $hostAndPort = IP
::splitHostAndPort( $realServer );
69 $realServer = $hostAndPort[0];
70 if ( $hostAndPort[1] ) {
71 $port = $hostAndPort[1];
73 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
74 // If we have a colon and something that's not a port number
75 // inside the hostname, assume it's the socket location
76 $hostAndSocket = explode( ':', $realServer );
77 $realServer = $hostAndSocket[0];
78 $socket = $hostAndSocket[1];
81 $mysqli = mysqli_init();
84 if ( $this->mFlags
& self
::DBO_SSL
) {
85 $connFlags |
= MYSQLI_CLIENT_SSL
;
94 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
95 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
97 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
98 $realServer = 'p:' . $realServer;
101 if ( $this->utf8Mode
) {
102 // Tell the server we're communicating with it in UTF-8.
103 // This may engage various charset conversions.
104 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
106 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
108 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
110 if ( $mysqli->real_connect( $realServer, $this->mUser
,
111 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
119 protected function connectInitCharset() {
120 // already done in mysqlConnect()
125 * @param string $charset
128 protected function mysqlSetCharset( $charset ) {
129 $conn = $this->getBindingHandle();
131 if ( method_exists( $conn, 'set_charset' ) ) {
132 return $conn->set_charset( $charset );
134 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
141 protected function closeConnection() {
142 $conn = $this->getBindingHandle();
144 return $conn->close();
150 function insertId() {
151 $conn = $this->getBindingHandle();
153 return (int)$conn->insert_id
;
159 function lastErrno() {
160 if ( $this->mConn
) {
161 return $this->mConn
->errno
;
163 return mysqli_connect_errno();
170 function affectedRows() {
171 $conn = $this->getBindingHandle();
173 return $conn->affected_rows
;
180 function selectDB( $db ) {
181 $conn = $this->getBindingHandle();
183 $this->mDBname
= $db;
185 return $conn->select_db( $db );
192 protected function mysqlFreeResult( $res ) {
202 protected function mysqlFetchObject( $res ) {
203 $object = $res->fetch_object();
204 if ( $object === null ) {
215 protected function mysqlFetchArray( $res ) {
216 $array = $res->fetch_array();
217 if ( $array === null ) {
228 protected function mysqlNumRows( $res ) {
229 return $res->num_rows
;
236 protected function mysqlNumFields( $res ) {
237 return $res->field_count
;
245 protected function mysqlFetchField( $res, $n ) {
246 $field = $res->fetch_field_direct( $n );
248 // Add missing properties to result (using flags property)
249 // which will be part of function mysql-fetch-field for backward compatibility
250 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
251 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
252 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
253 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
254 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
255 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
256 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
257 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
258 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
264 * @param resource|ResultWrapper $res
268 protected function mysqlFieldName( $res, $n ) {
269 $field = $res->fetch_field_direct( $n );
275 * @param resource|ResultWrapper $res
279 protected function mysqlFieldType( $res, $n ) {
280 $field = $res->fetch_field_direct( $n );
286 * @param resource|ResultWrapper $res
290 protected function mysqlDataSeek( $res, $row ) {
291 return $res->data_seek( $row );
295 * @param mysqli $conn Optional connection object
298 protected function mysqlError( $conn = null ) {
299 if ( $conn === null ) {
300 return mysqli_connect_error();
307 * Escapes special characters in a string for use in an SQL statement
311 protected function mysqlRealEscapeString( $s ) {
312 $conn = $this->getBindingHandle();
314 return $conn->real_escape_string( $s );
318 * Give an id for the connection
320 * mysql driver used resource id, but mysqli objects cannot be cast to string.
323 public function __toString() {
324 if ( $this->mConn
instanceof mysqli
) {
325 return (string)$this->mConn
->thread_id
;
327 // mConn might be false or something.
328 return (string)$this->mConn
;