CodeIgniter: How to know if $this->load->database(); did connect?
If I configure my CI application to use database for session handling, if a session userdata returns false, I can assume that the user's session has expired.
But开发者_运维百科 what if the actual reason that it returned false was because the database connection was not established successfully?
By default CodeIgniter will fatal error if the database is loaded and cannot connect, but if you have:
$db['local']['db_debug'] = FALSE;
then it won't tell you. You can turn debugging off in general but still react to a failed load by doing this:
if ( $this->load->database() === FALSE )
{
exit('THE END IS NIGH!');
}
I test all I found and nothing wokrs, the only way I found was with dbutil checking if database exists.
$this->load->database();
$this->load->dbutil();
// check connection details
if( !$this->dbutil->database_exists('myDatabase'))
echo 'Not connected to a database, or database not exists';
CI throws
A Database Error Occurred
Unable to connect to your database server using the provided settings.
or something similar on connection failure
精彩评论