3 * This is the MySQL 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 mysql.
30 class DatabaseMysql
extends DatabaseMysqlBase
{
33 * @return resource False on error
35 protected function doQuery( $sql ) {
36 $conn = $this->getBindingHandle();
38 if ( $this->bufferResults() ) {
39 $ret = mysql_query( $sql, $conn );
41 $ret = mysql_unbuffered_query( $sql, $conn );
48 * @param string $realServer
49 * @return bool|resource MySQL Database connection or false on failure to connect
50 * @throws DBConnectionError
52 protected function mysqlConnect( $realServer ) {
53 # Avoid a suppressed fatal error, which is very hard to track down
54 if ( !extension_loaded( 'mysql' ) ) {
55 throw new DBConnectionError(
57 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
62 if ( $this->mFlags
& DBO_SSL
) {
63 $connFlags |
= MYSQL_CLIENT_SSL
;
65 if ( $this->mFlags
& DBO_COMPRESS
) {
66 $connFlags |
= MYSQL_CLIENT_COMPRESS
;
69 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
77 for ( $i = 0; $i < $numAttempts && !$conn; $i++
) {
81 if ( $this->mFlags
& DBO_PERSISTENT
) {
82 $conn = mysql_pconnect( $realServer, $this->mUser
, $this->mPassword
, $connFlags );
84 # Create a new connection...
85 $conn = mysql_connect( $realServer, $this->mUser
, $this->mPassword
, true, $connFlags );
93 * @param string $charset
96 protected function mysqlSetCharset( $charset ) {
97 $conn = $this->getBindingHandle();
99 if ( function_exists( 'mysql_set_charset' ) ) {
100 return mysql_set_charset( $charset, $conn );
102 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
109 protected function closeConnection() {
110 $conn = $this->getBindingHandle();
112 return mysql_close( $conn );
118 function insertId() {
119 $conn = $this->getBindingHandle();
121 return mysql_insert_id( $conn );
127 function lastErrno() {
128 if ( $this->mConn
) {
129 return mysql_errno( $this->mConn
);
131 return mysql_errno();
138 function affectedRows() {
139 $conn = $this->getBindingHandle();
141 return mysql_affected_rows( $conn );
148 function selectDB( $db ) {
149 $conn = $this->getBindingHandle();
151 $this->mDBname
= $db;
153 return mysql_select_db( $db, $conn );
156 protected function mysqlFreeResult( $res ) {
157 return mysql_free_result( $res );
160 protected function mysqlFetchObject( $res ) {
161 return mysql_fetch_object( $res );
164 protected function mysqlFetchArray( $res ) {
165 return mysql_fetch_array( $res );
168 protected function mysqlNumRows( $res ) {
169 return mysql_num_rows( $res );
172 protected function mysqlNumFields( $res ) {
173 return mysql_num_fields( $res );
176 protected function mysqlFetchField( $res, $n ) {
177 return mysql_fetch_field( $res, $n );
180 protected function mysqlFieldName( $res, $n ) {
181 return mysql_field_name( $res, $n );
184 protected function mysqlFieldType( $res, $n ) {
185 return mysql_field_type( $res, $n );
188 protected function mysqlDataSeek( $res, $row ) {
189 return mysql_data_seek( $res, $row );
192 protected function mysqlError( $conn = null ) {
193 return ( $conn !== null ) ?
mysql_error( $conn ) : mysql_error(); // avoid warning
196 protected function mysqlRealEscapeString( $s ) {
197 $conn = $this->getBindingHandle();
199 return mysql_real_escape_string( $s, $conn );
202 protected function mysqlPing() {
203 $conn = $this->getBindingHandle();
205 return mysql_ping( $conn );