Posted By

mladoux on 07/11/12


Tagged

database Installer codeigniter


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

daK76
Priestd09


Check Database Connection Details - CodeIgniter


 / Published in: PHP
 

URL: http://markladoux.com/

Quick and easy way to check database connection details for installer controllers.

  1. /**
  2.  * Check Database Connection
  3.  *
  4.  * Checks connection details to see if they are correct. Useful for testing
  5.  * user supplied details in an install controller.
  6.  *
  7.  * @access protected
  8.  * @param string $protocol protocol to connect with
  9.  * @param string $host host to connect to
  10.  * @param string $user username to login with
  11.  * @param string $pass password to login with
  12.  * @param string $database database to check for
  13.  * @param integer $port port to connect on (optional)
  14.  * @return bool
  15.  */
  16. protected function _check_db($protocol,$host,$user,$password,$database,$port = NULL)
  17. {
  18. // prep the DSN string
  19. $dsn = "{$protocol}://{$user}:{$password}@{$host}/{$database}";
  20. if($port !== NULL)
  21. {
  22. $dsn .="?port={$port}";
  23. }
  24.  
  25. // Load the database and dbutil
  26. $this->load->database($dsn);
  27. $this->load->dbutil();
  28.  
  29. // check the connection details
  30. $check = $this->dbutil->database_exists($database);
  31.  
  32. // close the database
  33. $this->db->close();
  34.  
  35. // return our status
  36. return $check;
  37. }

Report this snippet  

You need to login to post a comment.