Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current PDO driver from existing connection?

Tags:

php

pdo

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.

like image 779
FtDRbwLXw6 Avatar asked Apr 10 '12 14:04

FtDRbwLXw6


1 Answers

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

$name = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);
like image 93
Tim Cooper Avatar answered Sep 18 '22 18:09

Tim Cooper