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
23 namespace Wikimedia\Rdbms
;
31 * Database abstraction object for PHP extension mysqli.
37 class DatabaseMysqli
extends DatabaseMysqlBase
{
40 * @return mysqli_result|bool
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
55 * @param string $realServer
56 * @param string|null $dbName
58 * @throws DBConnectionError
60 protected function mysqlConnect( $realServer, $dbName ) {
61 if ( !function_exists( 'mysqli_init' ) ) {
62 throw $this->newExceptionAfterConnectError(
63 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
72 $hostAndPort = IP
::splitHostAndPort( $realServer );
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
84 $mysqli = mysqli_init();
85 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
86 // of whether any column values changed. This is what callers want to know and is
87 // consistent with what Postgres, SQLite, and SQL Server return.
88 $connFlags = MYSQLI_CLIENT_FOUND_ROWS
;
89 if ( $this->getFlag( self
::DBO_SSL
) ) {
90 $connFlags |
= MYSQLI_CLIENT_SSL
;
99 if ( $this->getFlag( self
::DBO_COMPRESS
) ) {
100 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
102 if ( $this->getFlag( self
::DBO_PERSISTENT
) ) {
103 $realServer = 'p:' . $realServer;
106 if ( $this->utf8Mode
) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
115 if ( $mysqli->real_connect(
133 protected function closeConnection() {
134 $conn = $this->getBindingHandle();
136 return $conn->close();
142 function insertId() {
143 $conn = $this->getBindingHandle();
145 return (int)$conn->insert_id
;
151 function lastErrno() {
152 if ( $this->conn
instanceof mysqli
) {
153 return $this->conn
->errno
;
155 return mysqli_connect_errno();
162 protected function fetchAffectedRowCount() {
163 $conn = $this->getBindingHandle();
165 return $conn->affected_rows
;
169 * @param mysqli_result $res
172 protected function mysqlFreeResult( $res ) {
179 * @param mysqli_result $res
180 * @return stdClass|bool
182 protected function mysqlFetchObject( $res ) {
183 $object = $res->fetch_object();
184 if ( $object === null ) {
192 * @param mysqli_result $res
193 * @return array|false
195 protected function mysqlFetchArray( $res ) {
196 $array = $res->fetch_array();
197 if ( $array === null ) {
205 * @param mysqli_result $res
208 protected function mysqlNumRows( $res ) {
209 return $res->num_rows
;
213 * @param mysqli_result $res
216 protected function mysqlNumFields( $res ) {
217 return $res->field_count
;
221 * @param mysqli_result $res
225 protected function mysqlFetchField( $res, $n ) {
226 $field = $res->fetch_field_direct( $n );
228 // Add missing properties to result (using flags property)
229 // which will be part of function mysql-fetch-field for backward compatibility
230 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
231 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
232 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
233 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
234 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
235 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
236 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
237 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
238 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
244 * @param mysqli_result $res
248 protected function mysqlFieldName( $res, $n ) {
249 $field = $res->fetch_field_direct( $n );
255 * @param mysqli_result $res
259 protected function mysqlFieldType( $res, $n ) {
260 $field = $res->fetch_field_direct( $n );
266 * @param mysqli_result $res
270 protected function mysqlDataSeek( $res, $row ) {
271 return $res->data_seek( $row );
275 * @param mysqli|null $conn Optional connection object
278 protected function mysqlError( $conn = null ) {
279 if ( $conn === null ) {
280 return mysqli_connect_error();
287 * Escapes special characters in a string for use in an SQL statement
291 protected function mysqlRealEscapeString( $s ) {
292 $conn = $this->getBindingHandle();
294 return $conn->real_escape_string( (string)$s );
300 protected function getBindingHandle() {
301 return parent
::getBindingHandle();
306 * @deprecated since 1.29
308 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );