Ejemplo n.º 1
0
/**
 * Connect to the database
 *
 * @param string $db_host
 * @param string $db_user
 * @param string $db_pass
 * @param string $db_name
 * @param boolean $halt_on_error If it is TRUE, the script will halt in case of error
 * @return array
 */
function dbconnect($db_host, $db_user, $db_pass, $db_name, $halt_on_error = TRUE)
{
    $connection_success = TRUE;
    $dbselection_success = TRUE;
    try {
        DatabaseFactory::connect($db_host, $db_user, $db_pass, $db_name, array('debug' => DatabaseFactory::isDebug('default')));
    } catch (\Exception $e) {
        $connection_success = $e instanceof SelectionException;
        $dbselection_success = FALSE;
        if ($halt_on_error and !$connection_success) {
            die("<strong>Unable to establish connection to MySQL</strong><br />" . $e->getCode() . " : " . $e->getMessage());
        } elseif ($halt_on_error) {
            die("<strong>Unable to select MySQL database</strong><br />" . $e->getCode() . " : " . $e->getMessage());
        }
    }
    return array('connection_success' => $connection_success, 'dbselection_success' => $dbselection_success);
}