php CodeIgniter检查数据库连接是否正确
/** * Check Database Connection * * Checks connection details to see if they are correct. Useful for testing * user supplied details in an install controller. * * @access protected * @param string $protocol protocol to connect with * @param string $host host to connect to * @param string $user username to login with * @param string $pass password to login with * @param string $database database to check for * @param integer $port port to connect on (optional) * @return bool */ protected function _check_db($protocol,$host,$user,$password,$database,$port = NULL) { // prep the DSN string $dsn = "{$protocol}://{$user}:{$password}@{$host}/{$database}"; if($port !== NULL) { $dsn .="?port={$port}"; } // Load the database and dbutil $this->load->database($dsn); $this->load->dbutil(); // check the connection details $check = $this->dbutil->database_exists($database); // close the database $this->db->close(); // return our status return $check; }