CodeIgniter Forums
Testing database connections - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Testing database connections (/showthread.php?tid=1662)



Testing database connections - BenHeadrick - 03-30-2015

Hello, I'm still learning CodeIgniter so please forgive me if my question is ridiculous. In a previous version of CI, I would set the 'autoinit' variable in the database config file to false, and initialize my database in an if statement which would return true or false if the connection couldn't be made without throwing any warnings. If false I would load a page giving instructions on how to set the database config file. I'm not seeing a choice for autoinit in CI 3, so I assume I need to find another solution. Could anyone point me in the right direction? Thanks!


RE: Testing database connections - CroNiX - 03-31-2015

It looks like they removed the option and it "autoinits" when it gets loaded. So don't autoload it in config/autoload.php, do your checks and then use $this->load->database(); to load it AND init it. http://www.codeigniter.com/userguide3/database/connecting.html


RE: Testing database connections - BenHeadrick - 03-31-2015

That makes sense. The following is working for me as far as I can tell. Anyone see any problems?
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 )
            {
                return 
true;
            }
            else{
                return 
false;
            }
        }
        else
        {
            return 
false;
        }
    } 



RE: Testing database connections - CroNiX - 04-01-2015

Why not just use $this->load->database(), which does everything you are doing manually and more?

PHP Code:
$this->load->database(); //loads "default" group config from /config/database.php
//$this->db is now using the default database connection

$this->load->database('group_two'); //loads "group_two" group config from /config/database.php
//$this->db is now using the "group_two" database connection 
http://www.codeigniter.com/userguide3/database/connecting.html


RE: Testing database connections - silentium - 04-01-2015

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
;




RE: Testing database connections - BenHeadrick - 04-03-2015

Thanks for the tips guys. I tried to find a way to get $this->load->database(); to work but didn't have any luck. With display errors set to off and db_debug set to false, I was able to prevent the error page, but couldn't find a way to test the connection like I could with $mysqli->connect_error. Too bad. It would be more versatile and I could test the connection with other drivers if I could use a built in CI function.

Anyway, Silentium, I took your advice on closing the connection and good call on the else statements. I'm still learning. Thanks again guys!