diff --git a/includes/database.inc b/includes/database.inc index 683ae69..992e5a7 100644 --- a/includes/database.inc +++ b/includes/database.inc @@ -117,10 +117,12 @@ function db_prefix_tables($sql) { * @param $name * The name assigned to the newly active database connection. If omitted, the * default connection will be made active. + * @param $abort + * If database connection could not be made, should we abort here or continue. * * @return the name of the previously active database or FALSE if non was found. */ -function db_set_active($name = 'default') { +function db_set_active($name = 'default', $abort = TRUE) { global $db_url, $db_type, $active_db; static $db_conns, $active_name = FALSE; @@ -148,7 +150,27 @@ function db_set_active($name = 'default') { _db_error_page("The database type '". $db_type ."' is unsupported. Please use either 'mysql' or 'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases."); } - $db_conns[$name] = db_connect($connect_url); + $connection = db_connect($connect_url); + // db_connect is using array return notation. + if (is_array($connection)) { + list($success, $result) = $connection; + // Connection worked + if ($success) { + $db_conns[$name] = $result; + } + // Connection failed and abort page load. + elseif ($abort) { + _db_error_page($result); + } + // Connection failed but return false. Calling function will handle error. + else { + return FALSE; + } + } + // db_connect is not using array return notation; revert to old code style. + else { + $db_conns[$name] = $connection; + } } $previous_name = $active_name; diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 01a7d17..4232c1d 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -76,7 +76,7 @@ function db_connect($url) { $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2); if (!$connection || !mysql_select_db(substr($url['path'], 1))) { // Show error screen otherwise - _db_error_page(mysql_error()); + return array(FALSE, mysql_error()); } // Force MySQL to use the UTF-8 character set. Also set the collation, if a @@ -89,7 +89,7 @@ function db_connect($url) { mysql_query('SET NAMES utf8', $connection); } - return $connection; + return array(TRUE, $connection); } /** @@ -176,7 +176,7 @@ function db_fetch_array($result) { * * @param $result * A database query result resource, as returned from db_query(). - * + * * @return * The resulting field or FALSE. */ diff --git a/includes/database.mysqli.inc b/includes/database.mysqli.inc index 613ff9e..cb8c0a0 100644 --- a/includes/database.mysqli.inc +++ b/includes/database.mysqli.inc @@ -75,7 +75,7 @@ function db_connect($url) { @mysqli_real_connect($connection, $url['host'], $url['user'], $url['pass'], substr($url['path'], 1), $url['port'], NULL, MYSQLI_CLIENT_FOUND_ROWS); if (mysqli_connect_errno() > 0) { - _db_error_page(mysqli_connect_error()); + return array(FALSE, mysqli_connect_error()); } // Force MySQL to use the UTF-8 character set. Also set the collation, if a @@ -88,7 +88,7 @@ function db_connect($url) { mysqli_query($connection, 'SET NAMES utf8'); } - return $connection; + return array(TRUE, $connection); } /** diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc index 5fb0ccc..f877c13 100644 --- a/includes/database.pgsql.inc +++ b/includes/database.pgsql.inc @@ -78,14 +78,14 @@ function db_connect($url) { $connection = @pg_connect($conn_string); if (!$connection) { require_once './includes/unicode.inc'; - _db_error_page(decode_entities($php_errormsg)); + return array(FALSE, decode_entities($php_errormsg)); } // Restore error tracking setting ini_set('track_errors', $track_errors_previous); pg_query($connection, "set client_encoding=\"UTF8\""); - return $connection; + return array(TRUE, $connection); } /**