In my webapp, I’m going to accept all database connection parameters (such as username, server, database, etc.) as input from users and before saving this data, I want to quickly check whether it is possible to get the connection successfully on basis of these connection parameters.
Here is my code:
$config = new \Doctrine\DBAL\Configuration();
$url = "mysql://user:pass@server/instance";
$connectionParams = array('url' => $url);
try {
$conn = \Doctrine\DBAL\DriverManager::getConnection ($connectionParams, $config);
if ($conn->connect()) {
echo "Connection Successful";
}
}
catch (Exception $e)
{
echo "Connection unsuccessful";
}
But I get an HTTP 500 error message when called connect(). My question is, how can I check if the connection parameters are valid?
source
share