Since the code is not made by you, I recommend you to put the source in order to see if there is more documentation of that, on the other hand, I see that you have captured the exceptions but the error is not printed clearly:
try
{
$connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
var_dump("Error: ".$e->getMessage());die; // Muestra el error que se produce
}
So you can find out what error is obtained and correct it.
Here is the documentation for the class PDO
Luck.