38

Is there a PHP command I can use to determine if PDO is enabled or disabled?

I know I an manually run phpinfo() and eyeball it, but I have a script I run various web servers that display selected PHP configuration settings for the server.

So I am looking to see if there is a command I can use.

keepAlive
  • 6,369
  • 5
  • 24
  • 39
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

7 Answers7

62

The proper way of determining that will be using the extension_loaded function:-

if ( extension_loaded('pdo') ) {
    .......
}

And you might also want to check for the database-specific PDO driver using:-

if ( extension_loaded('pdo_<database type here>') ) { // e.g., pdo_mysql
    .......
}
Samuel Katz
  • 24,066
  • 8
  • 71
  • 57
41

Check if the class exists:

if (class_exists('PDO'))

I appreciate the support and all the upvotes I still get, but please check Salman Abbas's answer for the proper way to do this.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Was going to suggest `defined()` but PDO switched to class constants. – AJ. May 24 '11 at 16:00
  • I tested it and this way works perfectly (both on a server that had it enabled and on a server that did not). Thanks @AJ! – H. Ferrence May 24 '11 at 16:03
  • 3
    @AJ: wouldn't it be better to add `false` as 2nd param? If the OP only purely wants to check for existance, but not to autoload the class at the same time, I mean. – Jürgen Thelen May 24 '11 at 16:08
  • 9
    Unless some insane library developer decided to name his *poly-device operator* class `PDO`. – webbiedave May 24 '11 at 16:22
14

Just run the command as php -m from the command prompt which will display list of modules installed for PHP

Prashant Agrawal
  • 381
  • 3
  • 14
7

You have two options:

if (extension_loaded('pdo')) { /* ... */ }

Or (this one is not 100% reliable since it can be implemented in user-land classes):

if (class_exists('PDO', false)) { /* ... */ }

Personally, I prefer the first option.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
3
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO available';
}

I hope this works

krishna
  • 930
  • 1
  • 16
  • 35
1

How about

if (in_array('pdo', get_loaded_extensions())) {
   ... pdo is there ...
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

For checking it on linux terminal level using above command

php -m

which would give output related to modules installed from your php.inienter image description here

saurabh kamble
  • 1,510
  • 2
  • 25
  • 42