user975718
user975718

Reputation: 91

How to detect if the MySQL service (or simply MySQL) is available with PHP?

How can I do that? like:

if (mysql is available) {
   connect and do queries
}
else {
   use files
}

Upvotes: 2

Views: 7106

Answers (5)

Dr Casper Black
Dr Casper Black

Reputation: 7478

Not that elegant:

if (shell_exec('mysql -V') != ''){
  echo "MySql is here";
}else{
  echo "No MySql here";
}

You may search for mysql in the string that "shell_exec('mysql -V')" will return.

Upvotes: 4

Romain Bruckert
Romain Bruckert

Reputation: 2610

Or use the phpinfo() function and search "mysql" with CTRL+F and check if the extension is activated...

Upvotes: 1

Grim...
Grim...

Reputation: 16953

I can't test this, but

if (function_exists('mysql_connect'))

may work.

Upvotes: 2

xdazz
xdazz

Reputation: 160863

Just try to connect and if fail then do other thing.

eg:

if ($link = mysql_connect('localhost', 'mysql_user', 'mysql_password')) {
    //do queries
} else {
   //use files
}

Upvotes: 0

Jonathan
Jonathan

Reputation: 5547

Have you tried:

extension_loaded("mysql");

http://us.php.net/extension_loaded

Upvotes: 3

Related Questions