4

In my webapp, I am going to accept all the database connection parameters (such as username, server, database etc.) as input from the users and before saving those information, I want to quickly test if a connection can be obtained successfully based on those 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 am getting HTTP 500 error at connect() call. My question is, how can I test if connection paremeters are valid?

hashbrown
  • 3,438
  • 1
  • 19
  • 37
  • Only by attempting to connect and see if an error is raised, as you are already doing. The error is driver-specific, but its message is often detailed enough to tell you the specific problem (e.g. "connection refused", "access denied"). – Matt S May 02 '17 at 17:52

0 Answers0