How to test Doalrine DBAL connection successfully

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"; // INFORMATION FROM USER
$connectionParams = array('url' => $url);

    try {
        $conn = \Doctrine\DBAL\DriverManager::getConnection ($connectionParams, $config);   

        if ($conn->connect()) { // GETTING ERROR HERE
            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?

+4
source share

Source: https://habr.com/ru/post/1619894/


All Articles