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
{
39 protected function doQuery( $sql ) {
40 $conn = $this->getBindingHandle();
42 if ( $this->bufferResults() ) {
43 $ret = $conn->query( $sql );
45 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
52 * @param string $realServer
54 * @throws DBConnectionError
56 protected function mysqlConnect( $realServer ) {
59 # Avoid suppressed fatal error, which is very hard to track down
60 if ( !function_exists( 'mysqli_init' ) ) {
61 throw new DBConnectionError( $this, "MySQLi functions missing,"
62 . " have you compiled PHP with the --with-mysqli option?\n" );
65 // Other than mysql_connect, mysqli_real_connect expects an explicit port
66 // and socket parameters. So we need to parse the port and socket out of
70 $hostAndPort = IP
::splitHostAndPort( $realServer );
72 $realServer = $hostAndPort[0];
73 if ( $hostAndPort[1] ) {
74 $port = $hostAndPort[1];
76 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
77 // If we have a colon and something that's not a port number
78 // inside the hostname, assume it's the socket location
79 $hostAndSocket = explode( ':', $realServer );
80 $realServer = $hostAndSocket[0];
81 $socket = $hostAndSocket[1];
84 $mysqli = mysqli_init();
87 if ( $this->mFlags
& DBO_SSL
) {
88 $connFlags |
= MYSQLI_CLIENT_SSL
;
97 if ( $this->mFlags
& DBO_COMPRESS
) {
98 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
100 if ( $this->mFlags
& DBO_PERSISTENT
) {
101 $realServer = 'p:' . $realServer;
105 // Tell the server we're communicating with it in UTF-8.
106 // This may engage various charset conversions.
107 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
111 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
113 if ( $mysqli->real_connect( $realServer, $this->mUser
,
114 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
122 protected function connectInitCharset() {
123 // already done in mysqlConnect()
128 * @param string $charset
131 protected function mysqlSetCharset( $charset ) {
132 $conn = $this->getBindingHandle();
134 if ( method_exists( $conn, 'set_charset' ) ) {
135 return $conn->set_charset( $charset );
137 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
144 protected function closeConnection() {
145 $conn = $this->getBindingHandle();
147 return $conn->close();
153 function insertId() {
154 $conn = $this->getBindingHandle();
156 return (int)$conn->insert_id
;
162 function lastErrno() {
163 if ( $this->mConn
) {
164 return $this->mConn
->errno
;
166 return mysqli_connect_errno();
173 function affectedRows() {
174 $conn = $this->getBindingHandle();
176 return $conn->affected_rows
;
183 function selectDB( $db ) {
184 $conn = $this->getBindingHandle();
186 $this->mDBname
= $db;
188 return $conn->select_db( $db );
195 protected function mysqlFreeResult( $res ) {
205 protected function mysqlFetchObject( $res ) {
206 $object = $res->fetch_object();
207 if ( $object === null ) {
218 protected function mysqlFetchArray( $res ) {
219 $array = $res->fetch_array();
220 if ( $array === null ) {
231 protected function mysqlNumRows( $res ) {
232 return $res->num_rows
;
239 protected function mysqlNumFields( $res ) {
240 return $res->field_count
;
248 protected function mysqlFetchField( $res, $n ) {
249 $field = $res->fetch_field_direct( $n );
251 // Add missing properties to result (using flags property)
252 // which will be part of function mysql-fetch-field for backward compatibility
253 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
254 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
255 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
256 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
257 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
258 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
259 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
260 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
261 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
267 * @param resource|ResultWrapper $res
271 protected function mysqlFieldName( $res, $n ) {
272 $field = $res->fetch_field_direct( $n );
278 * @param resource|ResultWrapper $res
282 protected function mysqlFieldType( $res, $n ) {
283 $field = $res->fetch_field_direct( $n );
289 * @param resource|ResultWrapper $res
293 protected function mysqlDataSeek( $res, $row ) {
294 return $res->data_seek( $row );
298 * @param mysqli $conn Optional connection object
301 protected function mysqlError( $conn = null ) {
302 if ( $conn === null ) {
303 return mysqli_connect_error();
310 * Escapes special characters in a string for use in an SQL statement
314 protected function mysqlRealEscapeString( $s ) {
315 $conn = $this->getBindingHandle();
317 return $conn->real_escape_string( $s );
321 * Give an id for the connection
323 * mysql driver used resource id, but mysqli objects cannot be cast to string.
326 public function __toString() {
327 if ( $this->mConn
instanceof mysqli
) {
328 return (string)$this->mConn
->thread_id
;
330 // mConn might be false or something.
331 return (string)$this->mConn
;