16

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
  • @witherwind it is not a duplicate. I am looking for a connection status method. The other question is looking to TEST a connection. Commented Feb 6, 2014 at 6:40

4 Answers 4

23

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

Then you get credit for saving it from oblivion (and helping me out). :)
6

$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)); } 

1 Comment

In my environment (PHP 8.1) the call to getAttribute on a "gone away" connection throws an exception. So something like this: try{ $x = $db->getAttribute(PDO::ATTR_SERVER_INFO); echo "OK"; } catch(Exception $e) { echo "need new connection"; }
5

you can use

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

Connections and Connection management
PDO::getAttribute

1 Comment

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

PDO::getAttribute - Retrieve a database connection attribute

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.