PHP/PDO: How to get the current connection status

纵然是瞬间 提交于 2019-12-30 08:13:54

问题


What is the PDO equivalent of:

mysqli_stat($dbConn);

P.S. I use it to (get a message to) make sure I am connected


回答1:


I cannot get credit for this answer. Someone posted the answer, but he/she latter deleted the entry.

Here's the (saved archived) answer to your question:

$status = $conn->getAttribute(PDO::ATTR_CONNECTION_STATUS);



回答2:


$pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS) always return "127.0.0.1 via TCP/IP" even if i stop mysqld, to use:

if ($pdo->getAttribute(PDO::ATTR_SERVER_INFO)=='MySQL server has gone away')
{
    $pdo=new PDO('mysql:host=127.0.0.1;port=3306;dbname=mydb;charset=UTF8', 'root', '', array(PDO::ATTR_PERSISTENT=>true));
}



回答3:


you can use

$name = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);

Connections and Connection management
PDO::getAttribute




回答4:


PDO::getAttribute - Retrieve a database connection attribute

http://www.php.net/manual/en/pdo.getattribute.php




回答5:


You can use this code to check your PDO connection

    /* PDO connection start */
    try {

        $conn = new PDO("mysql:host=$server; dbname=$sdosmsDB", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* PDO::ERRMODE_EXCEPTION, PDO::ERRMODE_SILENT, and PDO::ERRMODE_WARNING  */        
        $conn->exec("SET CHARACTER SET utf8"); 

    } catch(PDOException $e) {

        die( 'Database Connection failed: ' . $e->getMessage());

    }
    /* PDO connection end */


来源:https://stackoverflow.com/questions/21595402/php-pdo-how-to-get-the-current-connection-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!