Omar Juvera
Omar Juvera

Reputation: 12297

PHP/PDO: How to get the current connection status

What is the PDO equivalent of:

mysqli_stat($dbConn);

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

Upvotes: 16

Views: 28288

Answers (4)

Omar Juvera
Omar Juvera

Reputation: 12297

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

Upvotes: 23

diyism
diyism

Reputation: 12935

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

Upvotes: 6

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

you can use

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

Connections and Connection management
PDO::getAttribute

Upvotes: 5

Tan Hong Tat
Tan Hong Tat

Reputation: 6864

PDO::getAttribute - Retrieve a database connection attribute

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

Upvotes: 1

Related Questions