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 if ( $this->bufferResults() ) {
41 $ret = $this->mConn
->query( $sql );
43 $ret = $this->mConn
->query( $sql, MYSQLI_USE_RESULT
);
50 * @param string $realServer
52 * @throws DBConnectionError
54 protected function mysqlConnect( $realServer ) {
57 # Otherwise we get a suppressed fatal error, which is very hard to track down
58 if ( !function_exists( 'mysqli_init' ) ) {
59 throw new DBConnectionError( $this, "MySQLi functions missing,"
60 . " have you compiled PHP with the --with-mysqli option?\n" );
63 // Other than mysql_connect, mysqli_real_connect expects an explicit port
64 // and socket parameters. So we need to parse the port and socket out of
68 $hostAndPort = IP
::splitHostAndPort( $realServer );
70 $realServer = $hostAndPort[0];
71 if ( $hostAndPort[1] ) {
72 $port = $hostAndPort[1];
74 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
75 // If we have a colon and something that's not a port number
76 // inside the hostname, assume it's the socket location
77 $hostAndSocket = explode( ':', $realServer );
78 $realServer = $hostAndSocket[0];
79 $socket = $hostAndSocket[1];
83 if ( $this->mFlags
& DBO_SSL
) {
84 $connFlags |
= MYSQLI_CLIENT_SSL
;
86 if ( $this->mFlags
& DBO_COMPRESS
) {
87 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
89 if ( $this->mFlags
& DBO_PERSISTENT
) {
90 $realServer = 'p:' . $realServer;
93 $mysqli = mysqli_init();
95 // Tell the server we're communicating with it in UTF-8.
96 // This may engage various charset conversions.
97 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
99 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
101 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
103 if ( $mysqli->real_connect( $realServer, $this->mUser
,
104 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
112 protected function connectInitCharset() {
113 // already done in mysqlConnect()
118 * @param string $charset
121 protected function mysqlSetCharset( $charset ) {
122 if ( method_exists( $this->mConn
, 'set_charset' ) ) {
123 return $this->mConn
->set_charset( $charset );
125 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
132 protected function closeConnection() {
133 return $this->mConn
->close();
139 function insertId() {
140 return (int)$this->mConn
->insert_id
;
146 function lastErrno() {
147 if ( $this->mConn
) {
148 return $this->mConn
->errno
;
150 return mysqli_connect_errno();
157 function affectedRows() {
158 return $this->mConn
->affected_rows
;
165 function selectDB( $db ) {
166 $this->mDBname
= $db;
168 return $this->mConn
->select_db( $db );
175 protected function mysqlFreeResult( $res ) {
185 protected function mysqlFetchObject( $res ) {
186 $object = $res->fetch_object();
187 if ( $object === null ) {
198 protected function mysqlFetchArray( $res ) {
199 $array = $res->fetch_array();
200 if ( $array === null ) {
211 protected function mysqlNumRows( $res ) {
212 return $res->num_rows
;
219 protected function mysqlNumFields( $res ) {
220 return $res->field_count
;
228 protected function mysqlFetchField( $res, $n ) {
229 $field = $res->fetch_field_direct( $n );
231 // Add missing properties to result (using flags property)
232 // which will be part of function mysql-fetch-field for backward compatibility
233 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
234 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
235 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
236 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
237 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
238 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
239 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
240 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
241 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
247 * @param resource|ResultWrapper $res
251 protected function mysqlFieldName( $res, $n ) {
252 $field = $res->fetch_field_direct( $n );
258 * @param resource|ResultWrapper $res
262 protected function mysqlFieldType( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
269 * @param resource|ResultWrapper $res
273 protected function mysqlDataSeek( $res, $row ) {
274 return $res->data_seek( $row );
278 * @param mysqli $conn Optional connection object
281 protected function mysqlError( $conn = null ) {
282 if ( $conn === null ) {
283 return mysqli_connect_error();
290 * Escapes special characters in a string for use in an SQL statement
294 protected function mysqlRealEscapeString( $s ) {
295 return $this->mConn
->real_escape_string( $s );
298 protected function mysqlPing() {
299 return $this->mConn
->ping();
303 * Give an id for the connection
305 * mysql driver used resource id, but mysqli objects cannot be cast to string.
308 public function __toString() {
309 if ( $this->mConn
instanceof Mysqli
) {
310 return (string)$this->mConn
->thread_id
;
312 // mConn might be false or something.
313 return (string)$this->mConn
;