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 if ( $this->bufferResults() ) {
38 $ret = $this->mConn
->query( $sql );
40 $ret = $this->mConn
->query( $sql, MYSQLI_USE_RESULT
);
46 protected function mysqlConnect( $realServer ) {
49 # Otherwise we get a suppressed fatal error, which is very hard to track down
50 if ( !function_exists( 'mysqli_init' ) ) {
51 throw new DBConnectionError( $this, "MySQLi functions missing,"
52 . " have you compiled PHP with the --with-mysqli option?\n" );
56 if ( $this->mFlags
& DBO_SSL
) {
57 $connFlags |
= MYSQLI_CLIENT_SSL
;
59 if ( $this->mFlags
& DBO_COMPRESS
) {
60 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
62 if ( $this->mFlags
& DBO_PERSISTENT
) {
63 $realServer = 'p:' . $realServer;
66 $mysqli = mysqli_init();
68 // Tell the server we're communicating with it in UTF-8.
69 // This may engage various charset conversions.
70 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
72 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
76 for ( $i = 0; $i < $numAttempts; $i++
) {
80 if ( $mysqli->real_connect( $realServer, $this->mUser
,
81 $this->mPassword
, $this->mDBname
, null, null, $connFlags )
90 protected function connectInitCharset() {
91 // already done in mysqlConnect()
98 protected function mysqlSetCharset( $charset ) {
99 if ( method_exists( $this->mConn
, 'set_charset' ) ) {
100 return $this->mConn
->set_charset( $charset );
102 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
109 protected function closeConnection() {
110 return $this->mConn
->close();
116 function insertId() {
117 return $this->mConn
->insert_id
;
123 function lastErrno() {
124 if ( $this->mConn
) {
125 return $this->mConn
->errno
;
127 return mysqli_connect_errno();
134 function affectedRows() {
135 return $this->mConn
->affected_rows
;
142 function selectDB( $db ) {
143 $this->mDBname
= $db;
145 return $this->mConn
->select_db( $db );
151 function getServerVersion() {
152 return $this->mConn
->server_info
;
155 protected function mysqlFreeResult( $res ) {
161 protected function mysqlFetchObject( $res ) {
162 $object = $res->fetch_object();
163 if ( $object === null ) {
170 protected function mysqlFetchArray( $res ) {
171 $array = $res->fetch_array();
172 if ( $array === null ) {
179 protected function mysqlNumRows( $res ) {
180 return $res->num_rows
;
183 protected function mysqlNumFields( $res ) {
184 return $res->field_count
;
187 protected function mysqlFetchField( $res, $n ) {
188 $field = $res->fetch_field_direct( $n );
189 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
190 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
191 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
192 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
193 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
198 protected function mysqlFieldName( $res, $n ) {
199 $field = $res->fetch_field_direct( $n );
204 protected function mysqlFieldType( $res, $n ) {
205 $field = $res->fetch_field_direct( $n );
209 protected function mysqlDataSeek( $res, $row ) {
210 return $res->data_seek( $row );
213 protected function mysqlError( $conn = null ) {
214 if ( $conn === null ) {
215 return mysqli_connect_error();
221 protected function mysqlRealEscapeString( $s ) {
222 return $this->mConn
->real_escape_string( $s );
225 protected function mysqlPing() {
226 return $this->mConn
->ping();