Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php code to test pdo is available?

Tags:

php

mysql

pdo

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?

like image 564
ohho Avatar asked Jun 28 '10 09:06

ohho


People also ask

How do I know if PDO is enabled in PHP?

if ( extension_loaded('pdo_<database type here>') ) { // e.g., pdo_mysql ....... } Show activity on this post. 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. Was going to suggest defined() but PDO switched to class constants.

Is PDO included in PHP?

PDO is enabled by default as of php 5.1. 0 on most linux systems.

Does PHP 5.4 support PDO?

PDO will not run since upgrading PHP to 5.4.


2 Answers

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();  
like image 110
Elzo Valugi Avatar answered Sep 26 '22 14:09

Elzo Valugi


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

if (!defined('PDO::ATTR_DRIVER_NAME')) { echo 'PDO unavailable'; } 
like image 30
Mark Baker Avatar answered Sep 25 '22 14:09

Mark Baker