5

Possible Duplicate:
Detecting mysql support in php

Is there a quick way to programmatically check whether this particular PHP installation has been compiled with support for MYSQL?

Community
  • 1
  • 1
qdot
  • 6,025
  • 5
  • 42
  • 93
  • 4
    Do you mean phpinfo(); ? – Bono Jun 19 '12 at 08:48
  • phpinfo just renders a crapload of HTML. Surely there should be a way to just check the supported features from within my PHP script. – qdot Jun 19 '12 at 08:50
  • at the command prompt, type `php -m`. Programmatically: http://php.net/manual/en/function.get-loaded-extensions.php – Gordon Jun 19 '12 at 13:14

4 Answers4

9

There are actually multiple modules supporting MySQL (mysql, mysqli, pdo_mysql, ...). MySQLi (improved) is generally recommended for more complete support of MySQL5 features versus the original mysql module. PDO (PHP data objects) is a database abstraction layer that provides an object oriented data abstraction.

You can use function_exists() per the previous comments if you want to check for the existence of a specific function per module (mysql_connect, mysqli_connect, ...).

Alternatively, you can use the PHP function extension_loaded() to check for the extension itself (module name matching the output from phpinfo() ):

<?php
    if (extension_loaded('mysql') or extension_loaded('mysqli')) {
        // Looking good
    }
?>

From a command line prompt, you can list all compiled-in modules with:

php -m

If you're on a unix-ish system, use grep to filter the output to MySQL-related modules:

php -m | grep -i mysql

If you're on Windows, use findstr to filter the output to MySQL-related modules:

php -m | findstr -i mysql
Stennie
  • 61,270
  • 14
  • 141
  • 170
8
if (function_exists('mysql_connect')) ...
deceze
  • 491,798
  • 79
  • 706
  • 853
  • Agreed, but if there's a function named `mysql_connect`, it's likely *the* `mysql_connect` or something that works exactly the same. :) – deceze Jun 19 '12 at 13:11
  • Well, I just was saying so. And also I found the duplicate of this question which I think is worth to point to, too. – hakre Jun 19 '12 at 13:12
3

I think you might be looking for phpinfo();

This shows the info about PHP configuration.

phpinfo — Outputs information about PHP's configuration

Bono
  • 4,649
  • 6
  • 46
  • 76
  • Programmatically. Anyway, the other answers answered this particular question. – qdot Jun 19 '12 at 08:51
  • 1
    @qdot It's still programmatically, since it's a function and all that. But I get what you mean ;) – Bono Jun 19 '12 at 08:54
2

Yes, there is. Just check if one of the mysql_* functions exists, say mysql_connect:

if (function_exists("mysql_connect")) {
  echo "compiled using --with-mysql\n";
}
Carsten
  • 17,265
  • 3
  • 44
  • 52