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];
85 if ( $this->mFlags
& DBO_SSL
) {
86 $connFlags |
= MYSQLI_CLIENT_SSL
;
88 if ( $this->mFlags
& DBO_COMPRESS
) {
89 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
91 if ( $this->mFlags
& DBO_PERSISTENT
) {
92 $realServer = 'p:' . $realServer;
95 $mysqli = mysqli_init();
97 // Tell the server we're communicating with it in UTF-8.
98 // This may engage various charset conversions.
99 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
101 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
103 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
105 if ( $mysqli->real_connect( $realServer, $this->mUser
,
106 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
114 protected function connectInitCharset() {
115 // already done in mysqlConnect()
120 * @param string $charset
123 protected function mysqlSetCharset( $charset ) {
124 $conn = $this->getBindingHandle();
126 if ( method_exists( $conn, 'set_charset' ) ) {
127 return $conn->set_charset( $charset );
129 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
136 protected function closeConnection() {
137 $conn = $this->getBindingHandle();
139 return $conn->close();
145 function insertId() {
146 $conn = $this->getBindingHandle();
148 return (int)$conn->insert_id
;
154 function lastErrno() {
155 if ( $this->mConn
) {
156 return $this->mConn
->errno
;
158 return mysqli_connect_errno();
165 function affectedRows() {
166 $conn = $this->getBindingHandle();
168 return $conn->affected_rows
;
175 function selectDB( $db ) {
176 $conn = $this->getBindingHandle();
178 $this->mDBname
= $db;
180 return $conn->select_db( $db );
187 protected function mysqlFreeResult( $res ) {
197 protected function mysqlFetchObject( $res ) {
198 $object = $res->fetch_object();
199 if ( $object === null ) {
210 protected function mysqlFetchArray( $res ) {
211 $array = $res->fetch_array();
212 if ( $array === null ) {
223 protected function mysqlNumRows( $res ) {
224 return $res->num_rows
;
231 protected function mysqlNumFields( $res ) {
232 return $res->field_count
;
240 protected function mysqlFetchField( $res, $n ) {
241 $field = $res->fetch_field_direct( $n );
243 // Add missing properties to result (using flags property)
244 // which will be part of function mysql-fetch-field for backward compatibility
245 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
246 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
247 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
248 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
249 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
250 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
251 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
252 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
253 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
259 * @param resource|ResultWrapper $res
263 protected function mysqlFieldName( $res, $n ) {
264 $field = $res->fetch_field_direct( $n );
270 * @param resource|ResultWrapper $res
274 protected function mysqlFieldType( $res, $n ) {
275 $field = $res->fetch_field_direct( $n );
281 * @param resource|ResultWrapper $res
285 protected function mysqlDataSeek( $res, $row ) {
286 return $res->data_seek( $row );
290 * @param mysqli $conn Optional connection object
293 protected function mysqlError( $conn = null ) {
294 if ( $conn === null ) {
295 return mysqli_connect_error();
302 * Escapes special characters in a string for use in an SQL statement
306 protected function mysqlRealEscapeString( $s ) {
307 $conn = $this->getBindingHandle();
309 return $conn->real_escape_string( $s );
312 protected function mysqlPing() {
313 $conn = $this->getBindingHandle();
315 return $conn->ping();
319 * Give an id for the connection
321 * mysql driver used resource id, but mysqli objects cannot be cast to string.
324 public function __toString() {
325 if ( $this->mConn
instanceof Mysqli
) {
326 return (string)$this->mConn
->thread_id
;
328 // mConn might be false or something.
329 return (string)$this->mConn
;