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
);
47 * @param string $realServer
49 * @throws DBConnectionError
51 protected function mysqlConnect( $realServer ) {
54 # Otherwise we get a suppressed fatal error, which is very hard to track down
55 if ( !function_exists( 'mysqli_init' ) ) {
56 throw new DBConnectionError( $this, "MySQLi functions missing,"
57 . " have you compiled PHP with the --with-mysqli option?\n" );
61 if ( $this->mFlags
& DBO_SSL
) {
62 $connFlags |
= MYSQLI_CLIENT_SSL
;
64 if ( $this->mFlags
& DBO_COMPRESS
) {
65 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
67 if ( $this->mFlags
& DBO_PERSISTENT
) {
68 $realServer = 'p:' . $realServer;
71 $mysqli = mysqli_init();
73 // Tell the server we're communicating with it in UTF-8.
74 // This may engage various charset conversions.
75 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
77 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
81 for ( $i = 0; $i < $numAttempts; $i++
) {
85 if ( $mysqli->real_connect( $realServer, $this->mUser
,
86 $this->mPassword
, $this->mDBname
, null, null, $connFlags )
95 protected function connectInitCharset() {
96 // already done in mysqlConnect()
101 * @param string $charset
104 protected function mysqlSetCharset( $charset ) {
105 if ( method_exists( $this->mConn
, 'set_charset' ) ) {
106 return $this->mConn
->set_charset( $charset );
108 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
115 protected function closeConnection() {
116 return $this->mConn
->close();
122 function insertId() {
123 return $this->mConn
->insert_id
;
129 function lastErrno() {
130 if ( $this->mConn
) {
131 return $this->mConn
->errno
;
133 return mysqli_connect_errno();
140 function affectedRows() {
141 return $this->mConn
->affected_rows
;
148 function selectDB( $db ) {
149 $this->mDBname
= $db;
151 return $this->mConn
->select_db( $db );
157 function getServerVersion() {
158 return $this->mConn
->server_info
;
165 protected function mysqlFreeResult( $res ) {
175 protected function mysqlFetchObject( $res ) {
176 $object = $res->fetch_object();
177 if ( $object === null ) {
188 protected function mysqlFetchArray( $res ) {
189 $array = $res->fetch_array();
190 if ( $array === null ) {
201 protected function mysqlNumRows( $res ) {
202 return $res->num_rows
;
209 protected function mysqlNumFields( $res ) {
210 return $res->field_count
;
218 protected function mysqlFetchField( $res, $n ) {
219 $field = $res->fetch_field_direct( $n );
220 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
221 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
222 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
223 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
224 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
230 * @param resource|ResultWrapper $res
234 protected function mysqlFieldName( $res, $n ) {
235 $field = $res->fetch_field_direct( $n );
241 * @param resource|ResultWrapper $res
245 protected function mysqlFieldType( $res, $n ) {
246 $field = $res->fetch_field_direct( $n );
251 * @param resource|ResultWrapper $res
255 protected function mysqlDataSeek( $res, $row ) {
256 return $res->data_seek( $row );
260 * @param mysqli $conn Optional connection object
263 protected function mysqlError( $conn = null ) {
264 if ( $conn === null ) {
265 return mysqli_connect_error();
272 * Escapes special characters in a string for use in an SQL statement
276 protected function mysqlRealEscapeString( $s ) {
277 return $this->mConn
->real_escape_string( $s );
280 protected function mysqlPing() {
281 return $this->mConn
->ping();