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
{
37 protected function doQuery( $sql ) {
38 if ( $this->bufferResults() ) {
39 $ret = $this->mConn
->query( $sql );
41 $ret = $this->mConn
->query( $sql, MYSQLI_USE_RESULT
);
46 protected function mysqlConnect( $realServer ) {
48 # Otherwise we get a suppressed fatal error, which is very hard to track down
49 if ( !function_exists( 'mysqli_init' ) ) {
50 throw new DBConnectionError( $this, "MySQLi functions missing,"
51 . " have you compiled PHP with the --with-mysqli option?\n" );
55 if ( $this->mFlags
& DBO_SSL
) {
56 $connFlags |
= MYSQLI_CLIENT_SSL
;
58 if ( $this->mFlags
& DBO_COMPRESS
) {
59 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
61 if ( $this->mFlags
& DBO_PERSISTENT
) {
62 $realServer = 'p:' . $realServer;
65 $mysqli = mysqli_init();
68 for ( $i = 0; $i < $numAttempts; $i++
) {
72 if ( $mysqli->real_connect( $realServer, $this->mUser
,
73 $this->mPassword
, $this->mDBname
, null, null, $connFlags ) )
85 protected function mysqlSetCharset( $charset ) {
86 if ( method_exists( $this->mConn
, 'set_charset' ) ) {
87 return $this->mConn
->set_charset( $charset );
89 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
96 protected function closeConnection() {
97 return $this->mConn
->close();
103 function insertId() {
104 return $this->mConn
->insert_id
;
110 function lastErrno() {
111 if ( $this->mConn
) {
112 return $this->mConn
->errno
;
114 return mysqli_connect_errno();
121 function affectedRows() {
122 return $this->mConn
->affected_rows
;
129 function selectDB( $db ) {
130 $this->mDBname
= $db;
131 return $this->mConn
->select_db( $db );
137 function getServerVersion() {
138 return $this->mConn
->server_info
;
141 protected function mysqlFreeResult( $res ) {
146 protected function mysqlFetchObject( $res ) {
147 $object = $res->fetch_object();
148 if ( $object === null ) {
154 protected function mysqlFetchArray( $res ) {
155 $array = $res->fetch_array();
156 if ( $array === null ) {
162 protected function mysqlNumRows( $res ) {
163 return $res->num_rows
;
166 protected function mysqlNumFields( $res ) {
167 return $res->field_count
;
170 protected function mysqlFetchField( $res, $n ) {
171 $field = $res->fetch_field_direct( $n );
172 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
173 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
174 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
175 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
176 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
180 protected function mysqlFieldName( $res, $n ) {
181 $field = $res->fetch_field_direct( $n );
185 protected function mysqlDataSeek( $res, $row ) {
186 return $res->data_seek( $row );
189 protected function mysqlError( $conn = null ) {
190 if ($conn === null) {
191 return mysqli_connect_error();
197 protected function mysqlRealEscapeString( $s ) {
198 return $this->mConn
->real_escape_string( $s );
201 protected function mysqlPing() {
202 return $this->mConn
->ping();