Welcome Guest, Not a member yet? Register   Sign In
Testing database connections
#5

Using $this->load->database(); will throw a error page if the connection failed or for example the config file does not exists. And this behavior can not be overwritten.

From @BenHeadrick first post, that is exactly what he wants to get away from.

(03-30-2015, 10:24 PM)BenHeadrick Wrote: ... without throwing any warnings. ...

As far as I know, CI does not have a function that only check if it can connect to the database or not, and return true/false.

For the solution though, I would close the connection before returning true/false. And then later when you need to actually use the DB, you connect with $this->load->database(); so you can use CIs query builder and more as usual.

Here is an updated example. I also removed all else statement as they are not needed.
PHP Code:
public function check_database()
{
 
   
    ini_set
('display_errors''Off');
 
   
    
//  Load the database config file.
 
   if(file_exists($file_path APPPATH.'config/database.php'))
 
   {
 
       include($file_path);
 
   }
 
   
    $config 
$db[$active_group];
 
   
    
//  Check database connection if using mysqli driver
 
   if$config['dbdriver'] === 'mysqli' )
 
   {
 
       $mysqli = new mysqli$config['hostname'] , $config['username'] , $config['password'] , $config['database'] );
 
       if( !$mysqli->connect_error )
 
       {
 
           $mysqli->close();
 
           return true;
 
       }
 
       
        $mysqli
->close();
 
   }
 
   
    return false
;

Reply


Messages In This Thread
Testing database connections - by BenHeadrick - 03-30-2015, 10:24 PM
RE: Testing database connections - by CroNiX - 03-31-2015, 06:55 AM
RE: Testing database connections - by BenHeadrick - 03-31-2015, 04:04 PM
RE: Testing database connections - by CroNiX - 04-01-2015, 07:14 AM
RE: Testing database connections - by silentium - 04-01-2015, 10:26 AM
RE: Testing database connections - by BenHeadrick - 04-03-2015, 07:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB