From: Tim Starling Date: Wed, 4 Jun 2008 01:44:36 +0000 (+0000) Subject: Capture PHP errors from mysql_connect(). Apparently this is the only place connection... X-Git-Tag: 1.31.0-rc.0~47187 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=467e217e6a0585b032f8ae357103221f1f041c0b;p=lhc%2Fweb%2Fwiklou.git Capture PHP errors from mysql_connect(). Apparently this is the only place connection errors are available in PHP 5.2.6. --- diff --git a/includes/Database.php b/includes/Database.php index 67858f0c65..b23126534b 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -26,6 +26,7 @@ class Database { #------------------------------------------------------------------------------ protected $mLastQuery = ''; + protected $mPHPError = false; protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname; protected $mOut, $mOpened = false; @@ -343,21 +344,23 @@ class Database { # so we use a short timeout plus a manual retry. $this->mConn = false; $max = 3; + $this->installErrorHandler(); for ( $i = 0; $i < $max && !$this->mConn; $i++ ) { if ( $i > 1 ) { usleep( 1000 ); } if ( $this->mFlags & DBO_PERSISTENT ) { - @/**/$this->mConn = mysql_pconnect( $realServer, $user, $password ); + $this->mConn = mysql_pconnect( $realServer, $user, $password ); } else { # Create a new connection... - @/**/$this->mConn = mysql_connect( $realServer, $user, $password, true ); + $this->mConn = mysql_connect( $realServer, $user, $password, true ); } if ($this->mConn === false) { #$iplus = $i + 1; #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n"); } } + $phpError = $this->restoreErrorHandler(); wfProfileOut("dbconnect-$server"); @@ -396,7 +399,7 @@ class Database { // Turn off strict mode if it is on } else { - $this->reportConnectionError(); + $this->reportConnectionError( $phpError ); } $this->mOpened = $success; @@ -405,6 +408,20 @@ class Database { } /**@}}*/ + protected function installErrorHandler() { + $this->mPHPError = false; + set_error_handler( array( $this, 'connectionErrorHandler' ) ); + } + + protected function restoreErrorHandler() { + restore_error_handler(); + return $this->mPHPError; + } + + protected function connectionErrorHandler( $errno, $errstr ) { + $this->mPHPError = $errstr; + } + /** * Closes a database connection. * if it is open : commits any open transactions