How do i ping the MySQL db and reconnect using PDO

后端 未结 2 913
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 18:00

I am using MySQL PDO for handling database querying and all. But most of the time, the MySQL connection is gone away. So i am looking in the PDO that will check if the db co

2条回答
  •  盖世英雄少女心
    2020-12-17 18:48

    You can use like this:

    # connect to the database
    try {
      $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
      $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
    }
    catch(PDOException $e) {
        echo "Connection error " . $e->getMessage() . "\n";
        exit;
    }
    

提交回复
热议问题