How to verify if a DB exists before a connection attempt

I have just migrated to V2023. I love it. (registered user)I use the same script to connect to several DBs with the same structure. I would like to check if the DBname exists on the server before attempting the connection and customize the error message instead of getting the default DIE() message from PHPmaker.I NEED HELP WITH THE CONDITION TO PUT IN JUST 1 LINE (the second IF statement)

// Database Connecting event
function Database_Connecting(&$info)
{

if(isset($_POST['db_id'])){
	
$_SESSION['db'] = 'db_name_'.$_POST['db_id'];

}

// I NEED HELP WITH THE CONDITION TO PUT IN  THIS 1 LINE  BELOW

if( ******The  database $_SESSION['db'] does NOT exits****** ){

$_SESSION = array();
$_SESSION[SESSION_FAILURE_MESSAGE] = "Database ID not found";
return false;

}else{ 	$info["host"] 		= "locahost";
		$info["user"] 		= 'root';   
		$info["password"] 	= 'password';
		$info["dbname"] 	= $_SESSION['db'];  
	}
}

I simplified the code, but in real life I make sure that one User cannot switch to an unauthorized DB even if exist.

I don’t think there is such feature to check the database name first before attempting to connect to it.

Since the Linked Tables is a feature to connect to the different databases, then we assume that we have already known which database should we want to connect before creating the connection for that Linked Tables.

How to catch the connection failure error to customize it and redirect the user to the login page again (return false) smoothly?

You may customize the generated views/Error.php file that suits your needs. Or, perhaps you may create your own Extension to handle that situation.

Thank you.

I’ll have to make sure I do not genarate it again after customization.

wilmino wrote:

How to catch the connection failure error…

Since you have the connection info, you may try connect with the connection info in a catch block to test. But it may not be a good idea, you don’t want to connect unnecessarily. You better save the valid database names somewhere (e.g. in a .php file).

Hi all,

I still cannot CATCH the exception when the DB does not based on the id_db given by the user:PATH\vendor\doctrine\dbal\src\Driver\API\MySQL\ExceptionConverter.php(101): An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user ‘user_id_db_value’@‘localhost’ (using password: YES)There is someting very wrong with my code.

try {
      function(){if (isset($_SESSION['id_db'])){ ////// Verify id_db is POSTED and set as $_SESSION['id_db'] above
		$_SESSION['db']='dbname_'.$_SESSION['id_db'];
		$_SESSION['user']='mydbuser'.$_SESSION['id_db'];
		$_SESSION['pass']='mydbpass'.$_SESSION['id_db'];
		$info["host"] 		= "127.0.0.1";
		$info["user"] 		=$_SESSION['user'];  
		$info["password"] 	=$_SESSION['pass'];
		$info["dbname"] 	= $_SESSION['db'];  
	}} or  throw new Exception('foo');
    } catch (Exception $e) {
		$info[] = array();
		$_SESSION = array();
		$_SESSION[SESSION_FAILURE_MESSAGE] = "The DB ID is not found.";
        return false;
    } finally {
        return true;
    }

You have syntax error, you cannot put a function in your code like that.