Setting a connect timeout with PDO

后端 未结 2 1654
粉色の甜心
粉色の甜心 2020-11-30 12:19

I\'m using PDO to get data off a MySQL server. What I noticed is this: if the MySQL server is unavailable, it takes really (relatively) long for this code to return

相关标签:
2条回答
  • 2020-11-30 12:36

    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).

    0 讨论(0)
  • 2020-11-30 12:58
    $DBH = new PDO(
        "mysql:host=$host;dbname=$dbname", 
        $username, 
        $password,
        array(
            PDO::ATTR_TIMEOUT => 5, // in seconds
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );
    
    0 讨论(0)
提交回复
热议问题