dqouryz3595 2011-06-07 09:46
浏览 105
已采纳

PDO连接测试

I am writing an installer for one of my apps and I would like to be able to test some default database settings.

Is this possible using PDO to test valid and invalid database connections?

I have the following code:

try{
            $dbh = new pdo('mysql:host=127.0.0.1:3308;dbname=axpdb','admin','1234');
            die(json_encode(array('outcome' => true)));
        }catch(PDOException $ex){
            die(json_encode(array(
                'outcome' => false,
                'message' => 'Unable to connect'
            )));
        }

The problem I am having is that the script trys to connect until the script execution time of 60 seconds runs out instead of saying it cannot connect to the db.

Thanks

  • 写回答

4条回答 默认 最新

  • douzhang8144 2011-06-07 10:25
    关注

    you need to set the error mode when connection to the database:

    try{
        $dbh = new pdo( 'mysql:host=127.0.0.1:3308;dbname=axpdb',
                        'admin',
                        '1234',
                        array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        die(json_encode(array('outcome' => true)));
    }
    catch(PDOException $ex){
        die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
    }
    

    for more infos see the following links:

    Using MySQL with PDO

    Errors and error handling

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探