Home » Php » php – PDO Connection Test

php – PDO Connection Test

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am writing an installer for one of my apps and I would like to be able to test some default database settings.

Is this possible using PDO to test valid and invalid database connections?

I have the following code:

try{
            $dbh = new pdo('mysql:host=127.0.0.1:3308;dbname=axpdb','admin','1234');
            die(json_encode(array('outcome' => true)));
        }catch(PDOException $ex){
            die(json_encode(array(
                'outcome' => false,
                'message' => 'Unable to connect'
            )));
        }

The problem I am having is that the script trys to connect until the script execution time of 60 seconds runs out instead of saying it cannot connect to the db.

Thanks

How to&Answers:

you need to set the error mode when connection to the database:

try{
    $dbh = new pdo( 'mysql:host=127.0.0.1:3308;dbname=axpdb',
                    'admin',
                    '1234',
                    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    die(json_encode(array('outcome' => true)));
}
catch(PDOException $ex){
    die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
}

for more infos see the following links:

Using MySQL with PDO

Errors and error handling

Answer:

As @Sascha Galley already mentioned you should set error mode to exception mode. However, you should also set up PDO::ATTR_TIMEOUT attribute to prevent a long time waiting for response in some cases.

Although documentation says that behavior of this attribute is driver-dependent in case of MySQL it’s a connection timeout. You won’t find anything about it documentation but here’s a short snippet from driver’s source code:

long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);

Answer:

As seen e.g. in the comments at this answer (but hardly anywhere else, so I made it more visible here), the “classic” PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION solution does not always work.

The implementation of PDO::ERRMODE_EXCEPTION is broken, so it seems to be “leaking” in some cases.

For example:

Warning: PDO::__construct() [pdo.–construct]: [2002] No connection could be made because the target machine actively refused
it. (trying to connect via tcp://localhost:3306) in
[…] db.php on line 34

The code there:

try {
    $this->pdo = new PDO($cfg['DB'], $cfg['DB_USER'], $cfg['DB_PASS'],
        array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch {
    echo("Can't open the database.");
}

The exception is thrown (and cought: I can see my message).

So, as a necessary workaround, you need to also put a @ (let’s call it a “diaper operator” in this case) before new pdo(...) to actually keep it clean.

Answer:

There’s a missing closing parenthese at the end of PDO::ERRMODE_EXCEPTION.

Should be:

$this->pdo = new PDO($cfg['DB'], $cfg['DB_USER'], $cfg['DB_PASS'],
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));