Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 849
Omar Avatar asked Feb 06 '14 06:02

Omar


People also ask

Does PDO close connection?

The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted—you do this by assigning null to the variable that holds the object.

What is PDO connection in PHP?

The PDO represents a connection between PHP and a database server. The PDOStatement represents a prepared statement and, after the statement is executed, an associated result set. The PDOException represents an error raised by PDO.

How fetch data from database in PHP and display PDO?

Fetch data from a result set by calling one of the following fetch methods: To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method.


4 Answers

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);
like image 78
Omar Avatar answered Oct 17 '22 21:10

Omar


$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));
}
like image 23
diyism Avatar answered Oct 17 '22 22:10

diyism


you can use

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

Connections and Connection management
PDO::getAttribute

like image 5
Ferrakkem Bhuiyan Avatar answered Oct 17 '22 22:10

Ferrakkem Bhuiyan


PDO::getAttribute - Retrieve a database connection attribute

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

like image 1
Tan Hong Tat Avatar answered Oct 17 '22 21:10

Tan Hong Tat