如何检测PHP服务(或简称MySQL)是否可用于PHP?

How can I do that? like:

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

Have you tried:

extension_loaded("mysql");

http://us.php.net/extension_loaded

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
}

I can't test this, but

if (function_exists('mysql_connect'))

may work.

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

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.