PHP / PDO: how to get the current connection status

What is the equivalent of PDO:

mysqli_stat($dbConn); 

PS I use it (to get a message), make sure I'm connected

+7
php pdo connection database-connection
source share
5 answers

I can not get a loan for this answer. Someone sent a response, but he / she deleted the entry.

Here (saved archived) answer your question:

 $status = $conn->getAttribute(PDO::ATTR_CONNECTION_STATUS); 
+11
source share

you can use

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

Connections and Connection Management
PDO :: getAttribute

+3
source share

PDO::getAttribute - Get the database connection attribute

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

+1
source share

$ pdo-> getAttribute (PDO :: ATTR_CONNECTION_STATUS) always returns "127.0.0.1 over 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)); } 
+1
source share

This code can be used to test 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 */ 
0
source share

All Articles