Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the possible results of PDO::getAttribute(PDO::ATTR_CONNECTION_STATUS)

I have been looking in to this almost all day.. and can't seem to find the values returned anywhere. Can somebody tell me:

  1. What values do PDO::getAttribute(PDO::ATTR_CONNECTION_STATUS); return?
  2. Is it possible to rely on its result to determinate if the connection is still alive?(And eventually, what could I use to check if the connection is still alive?)
like image 731
DaGhostman Dimitrov Avatar asked Mar 26 '13 16:03

DaGhostman Dimitrov


People also ask

What is PDO :: PARAM_STR?

PDO::PARAM_STR. Represents SQL character data types. For an INOUT parameter, use the bitwise OR operator to append PDO::PARAM_INPUT_OUTPUT to the type of data being bound. Set the fourth parameter, length , to the maximum expected length of the output value.

What is the use of PDO in PHP?

What Is PDO? PDO in PHP offers a data-access abstraction layer, which means you can issue queries and fetch data using the same functions regardless of which database you're using. PDO isn't a database abstraction; it doesn't rewrite SQL or imitates features that aren't accessible.

What is new PDO in PHP?

The PHP Data Objects (PDO) defines a lightweight interface for accessing databases in PHP. It provides a data-access abstraction layer for working with databases in PHP. It defines consistent API for working with various database systems.

What is PDO class in PHP?

The PDO class ¶Represents a connection between PHP and a database server.


1 Answers

Finally! it turns out that the mysqli::ping() function could be implemented within PDO as follows:

class PDOExtended extends PDO {
    public function __construct($dsn, $user, $pass, $options = array())
    {
        $this->link = parent::__construct($dsn, $user, $pass, $options);
        $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
    }

    // some methods

    public function isConnected()
    {
        try {
            return (bool) $this->link->query('SELECT 1+1');
        } catch (PDOException $e) {
            return false;
        }
    }

    //some other methods
}

REASON:
PDO::query(); returns array containing the results or false, In the current case it won't return nothing, cuz the connection is dead and PDO should throw an exception at us. And that is what we are expecting. The catch block will return false and and will not stop the execution of our script. The query used

SELECT 1+1;

will return 2 always and it is good to rely on due to the fact that it is calculated on the DB side. No connection, no result! It is not an overkill because it is very simple query and most of the databases (on normal shared host) are on localhost it will not take more than 0.0000s which is not much of a performance issue. Have not tested it with transactions yet, but should do the trick still.

like image 189
DaGhostman Dimitrov Avatar answered Nov 14 '22 04:11

DaGhostman Dimitrov