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
{
36 protected function doQuery( $sql ) {
37 $conn = $this->getBindingHandle();
39 if ( $this->bufferResults() ) {
40 $ret = $conn->query( $sql );
42 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
49 * @param string $realServer
51 * @throws DBConnectionError
53 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];
82 if ( $this->mFlags
& DBO_SSL
) {
83 $connFlags |
= MYSQLI_CLIENT_SSL
;
85 if ( $this->mFlags
& DBO_COMPRESS
) {
86 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
88 if ( $this->mFlags
& DBO_PERSISTENT
) {
89 $realServer = 'p:' . $realServer;
92 $mysqli = mysqli_init();
94 // Tell the server we're communicating with it in UTF-8.
95 // This may engage various charset conversions.
96 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
98 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
100 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
102 if ( $mysqli->real_connect( $realServer, $this->mUser
,
103 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
111 protected function connectInitCharset() {
112 // already done in mysqlConnect()
117 * @param string $charset
120 protected function mysqlSetCharset( $charset ) {
121 $conn = $this->getBindingHandle();
123 if ( method_exists( $conn, 'set_charset' ) ) {
124 return $conn->set_charset( $charset );
126 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
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->mConn
) {
153 return $this->mConn
->errno
;
155 return mysqli_connect_errno();
162 function affectedRows() {
163 $conn = $this->getBindingHandle();
165 return $conn->affected_rows
;
172 function selectDB( $db ) {
173 $conn = $this->getBindingHandle();
175 $this->mDBname
= $db;
177 return $conn->select_db( $db );
184 protected function mysqlFreeResult( $res ) {
194 protected function mysqlFetchObject( $res ) {
195 $object = $res->fetch_object();
196 if ( $object === null ) {
207 protected function mysqlFetchArray( $res ) {
208 $array = $res->fetch_array();
209 if ( $array === null ) {
220 protected function mysqlNumRows( $res ) {
221 return $res->num_rows
;
228 protected function mysqlNumFields( $res ) {
229 return $res->field_count
;
237 protected function mysqlFetchField( $res, $n ) {
238 $field = $res->fetch_field_direct( $n );
240 // Add missing properties to result (using flags property)
241 // which will be part of function mysql-fetch-field for backward compatibility
242 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
243 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
244 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
245 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
246 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
247 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
248 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
249 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
250 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
256 * @param resource|ResultWrapper $res
260 protected function mysqlFieldName( $res, $n ) {
261 $field = $res->fetch_field_direct( $n );
267 * @param resource|ResultWrapper $res
271 protected function mysqlFieldType( $res, $n ) {
272 $field = $res->fetch_field_direct( $n );
278 * @param resource|ResultWrapper $res
282 protected function mysqlDataSeek( $res, $row ) {
283 return $res->data_seek( $row );
287 * @param mysqli $conn Optional connection object
290 protected function mysqlError( $conn = null ) {
291 if ( $conn === null ) {
292 return mysqli_connect_error();
299 * Escapes special characters in a string for use in an SQL statement
303 protected function mysqlRealEscapeString( $s ) {
304 $conn = $this->getBindingHandle();
306 return $conn->real_escape_string( $s );
309 protected function mysqlPing() {
310 $conn = $this->getBindingHandle();
312 return $conn->ping();
316 * Give an id for the connection
318 * mysql driver used resource id, but mysqli objects cannot be cast to string.
321 public function __toString() {
322 if ( $this->mConn
instanceof Mysqli
) {
323 return (string)$this->mConn
->thread_id
;
325 // mConn might be false or something.
326 return (string)$this->mConn
;