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

Omar
  • 11,783
  • 21
  • 84
  • 114
  • @witherwind it is not a duplicate. I am looking for a connection status method. The other question is looking to TEST a connection. – Omar Feb 06 '14 at 06:40

4 Answers4

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);
Omar
  • 11,783
  • 21
  • 84
  • 114
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));
}
diyism
  • 12,477
  • 5
  • 46
  • 46
5

you can use

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

Connections and Connection management
PDO::getAttribute

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
1

PDO::getAttribute - Retrieve a database connection attribute

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

Tan Hong Tat
  • 6,671
  • 2
  • 27
  • 25