Try using PHP's mysql_ping function:
echo @mysql_ping() ? 'true' : 'false';
You will need to prepend the "@" to suppose the MySQL Warnings you'll get for running this function without being connected to a database.
There are other ways as well, but it depends on the code that you're using.