28

I want to use PDO but I'm not sure whether my hosting has set it up properly.

How can I test, in PHP, whether it is setup and working for MySQL?

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
ohho
  • 50,879
  • 75
  • 256
  • 383

6 Answers6

40

PDO is always installed for php 5.1+. You can check for specific db drivers that are installed or not using phpinfo(); You could try to check for specific drivers using @Mark Baker idea and checking for specific constants;

var_dump(defined(PDO::MYSQL_ATTR_LOCAL_INFILE)); // mysql
var_dump(PDO::FB_ATTR_TIME_FORMAT)); // firebird

Note that not all drivers have specific constants defined so phpinfo() remains best solution.

Using command line you can check using:

$ php -m

As an alternative of phpinfo() you can use:

extension_loaded ('PDO' ); // returns boolean
// or
extension_loaded('pdo_mysql');
// or get all extensions and search for a specific one
get_loaded_extensions(); 
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
33

Aside from using phpinfo() to see if it's correctly listed

if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
16

Try this

print_r(PDO::getAvailableDrivers());

should give applications supported by PHP

Array ( [0] => mysql [1] => sqlite )
rizon
  • 8,127
  • 1
  • 27
  • 17
8

Using command line, for PDO:

php -m|grep -i pdo

For PDO with MySQL support:

php -m|grep -i pdo_mysql

To install php mysql support, search for the package name (Ubuntu):

apt-cache search php5*|grep mysql

And install it if not already did (Ubuntu):

sudo apt-get install php5-mysql
Tareq
  • 5,283
  • 2
  • 15
  • 18
  • It should be noted that these commands only work on *nix systems (this may not be immediately obvious to someone new to web server administration). there are some alternatives for the grep command, but sudo and apt-get are different animals. Yes, I know that this is obvious to most, but as I said, not to all. – Dave Morton May 12 '18 at 22:34
4

Create a file that contains

<?php

phpinfo();

?>

It will show you if PDO (and other features are enabled)

DrColossos
  • 12,656
  • 3
  • 46
  • 67
0

For Windows servers, I've found the following useful for checking which PDO extensions are loaded from the command prompt:

php -m|findstr -i pdo_

On my system, that command nets me the following results:

pdo_mysql PDO_ODBC pdo_pgsql pdo_sqlite

Dave Morton
  • 671
  • 6
  • 16