Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

How to catch the situation when the db connection is not valid

posted by mblarsen 8 years ago

Can someone tell me how to check if the host, username, database name all are valid and that the user is authorized.

If I for example enter an invalid/unauthorized user I receive this warning, where expected an exception, after invoking connectToDatabase().

Any suggestions?

Warning
-------
{doc_root}/install/install.php(157): fDatabase->getConnection()
/Library/WebServer/Documents/work-sites/mcs_ibob/core/trunk/libs/flourish/fDatabase.php(1498): fDatabase->connectToDatabase()
/Library/WebServer/Documents/work-sites/mcs_ibob/core/trunk/libs/flourish/fDatabase.php(580): mysqli_connect('127.0.0.1', 'test', '', 'development')
[internal function]
mysqli_connect(): (42000/1044): Access denied for user ''@'localhost' to database 'development'

I have never gone through and dissected the various warning messages for all of the supported database extensions to figure out which indicate the server is not reachable and which indicate authentication issues.

That said, if you are targeting just a single database/extension you should be able to use fCore::startErrorCapture() and fCore::stopErrorCapture() to capture the warnings that the database extension creates and determine what happened.

I do have the intention of finishing the work someone start on creating a completely cross-db timeout parameter for the purposes of fast fail-over. Adding code to fDatabase to determine if a network error or authentication error happened would definitely make such a system easier to write.

posted by wbond 8 years ago

That did the trick. Thanks!

posted by mblarsen 8 years ago