CodeIgniter Forums
Testing connection to database - 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: Testing connection to database (/showthread.php?tid=3132)



Testing connection to database - El Forum - 09-12-2007

[eluser]Unknown[/eluser]
Hi,
In a page I would like to have a user test fill in the connection parameters to a database and then test them to see if the database is online. Is there a way that I could do that with CodeIgniter ?
I' masking that because when I'm trying a database connection that doesn't work I get the default CI exception message.


Testing connection to database - El Forum - 09-12-2007

[eluser]ELRafael[/eluser]
In the user_guide, database configuration, try to use the db_debug.

Or, not do the autoload database, just load in your controller with a try{} catch{}...

MAYBE this works for you


Testing connection to database - El Forum - 09-12-2007

[eluser]Unknown[/eluser]
$conn = @mysql_connect("localhost","root","");
if(is_resource($conn)){
$this->load->database();
$this->load->dbutil();
$data['dbs'] = $this->dbutil->list_databases();
}else{
$data['dbs'] = array();
}
$this->load->view('welcome_message',$data);


Testing connection to database - El Forum - 09-12-2007

[eluser]Glen Swinfield[/eluser]
eovidiu's answer should work. You need to supress the error (@) that is generated when the dbase cannot connect, in order to then deal with it yourself - i.e. tell the user the dbase is not online.