Setting a connect timeout with PDO

Solution 1:

$DBH = new PDO(
    "mysql:host=$host;dbname=$dbname", 
    $username, 
    $password,
    array(
        PDO::ATTR_TIMEOUT => 5, // in seconds
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    )
);

Solution 2:

I'm using the DBLIB driver for PDO - and that doesn't support the passing of options (throws a warning).

To get round this, you can edit the connection_timeout setting in the FreeTDS config file which is located at /etc/freetds/freetds.conf (on Ubuntu).