![]() |
Check for database connection - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Check for database connection (/showthread.php?tid=36295) |
Check for database connection - El Forum - 11-27-2010 [eluser]pshah.mumbai[/eluser] I have created a custom library called "Startup" which checks/loads/initializes all the basic things that I need to have in the application. I need to do some custom handling if the database connection is not correct. The DB_driver class throws a exception and completely kills off the application. So I have set the db_debug flag to FALSE in database.php file. Code: $db['default']['db_debug'] = FALSE; And in the Startup library it checks if the connection is valid. Code: class Startup The problem is $CI->db->conn_id just checks if it is able to connect to server i.e. hostname, ports are valid. It does not tell if the username/password/dbname is valid or not. Is there any $CI->db method that tells that the username/password/dbname is valid ? Check for database connection - El Forum - 11-27-2010 [eluser]pshah.mumbai[/eluser] One ugly solution is $CI->db->query("SHOW TABLES"); If it returns NULL then there is some issue with the connection. ![]() Check for database connection - El Forum - 11-27-2010 [eluser]InsiteFX[/eluser] If I am not mistaken the Database will not connect if the user id and password are wrong! InsiteFX Check for database connection - El Forum - 11-27-2010 [eluser]pshah.mumbai[/eluser] Quote:If I am not mistaken the Database will not connect if the user id and password are wrong! That is true if $db['default']['db_debug'] = TRUE - it will throw an exception. But if $db['default']['db_debug'] = FALSE then it will silently fail. Now if I want to manually check for a valid connection then there is no way to do it. The $this->db->conn_id will be set if it is able to open connection to database. It does not indicate a valid database/username/password. Below is the code from database/DB_driver.php Code: function initialize() |