FtDRbwLXw6
FtDRbwLXw6

Reputation: 28929

Get current PDO driver from existing connection?

I have a class that accepts an existing PDO connection in the constructor:

class Foo {
    public function __construct(\PDO $conn = NULL) {
        // ...
    }

    // ...
}

My question is: is there a way to determine what driver an existing PDO connection is currently using (preferably from the list found here)? I didn't see anything in the API documentation.

For the curious, I'd like to know which driver is being used because functionality in my class is database-specific, so I'd like a way to validate that a connection being passed to it is of the proper type.

Upvotes: 21

Views: 5503

Answers (2)

user142162
user142162

Reputation:

You can use PDO::getAttribute() with PDO::ATTR_DRIVER_NAME:

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

Upvotes: 33

Brad
Brad

Reputation: 163438

Use getAttribute():

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

$pdo_object->getAttribute(PDO::ATTR_DRIVER_NAME);

Upvotes: 5

Related Questions