Skip to content

Commit

Permalink
MDL-47584 database SQL: add debug info on connection error notices
Browse files Browse the repository at this point in the history
  • Loading branch information
jboulen authored and Damyon Wiese committed Oct 29, 2014
1 parent 60c1108 commit 9b45b74
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 3 additions & 6 deletions lib/dml/mysqli_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,10 @@ public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dbop
if ($dbhost and !empty($this->dboptions['dbpersist'])) {
$dbhost = "p:$dbhost";
}
ob_start();
$this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
$dberr = ob_get_contents();
ob_end_clean();
$errorno = @$this->mysqli->connect_errno;
$this->mysqli = @new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);

if ($errorno !== 0) {
if ($this->mysqli->connect_errno !== 0) {
$dberr = $this->mysqli->connect_error;
$this->mysqli = null;
throw new dml_connection_exception($dberr);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/dmllib.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ function setup_DB() {
$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
} catch (moodle_exception $e) {
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
$body = "Connection error: ".$CFG->wwwroot.
"\n\nInfo:".
"\n\tError code: ".$e->errorcode.
"\n\tDebug info: ".$e->debuginfo.
"\n\tServer: ".$_SERVER['SERVER_NAME']." (".$_SERVER['SERVER_ADDR'].")";
if (file_exists($CFG->dataroot.'/emailcount')){
$fp = @fopen($CFG->dataroot.'/emailcount', 'r');
$content = @fread($fp, 24);
Expand All @@ -352,15 +357,15 @@ function setup_DB() {
//email directly rather than using messaging
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
$body);
$fp = @fopen($CFG->dataroot.'/emailcount', 'w');
@fwrite($fp, time());
}
} else {
//email directly rather than using messaging
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
$body);
$fp = @fopen($CFG->dataroot.'/emailcount', 'w');
@fwrite($fp, time());
}
Expand Down

0 comments on commit 9b45b74

Please sign in to comment.