2015-03-31 42 views
-1

我在那個項目中發生錯誤,它說無法連接數據庫 它給出下面的錯誤...如何處理這個錯誤?我不知道這是爲什麼發生.. 我導入數據庫到MySQL。如何處理數據庫服務器錯誤?

A Database Error Occurred 

Unable to connect to your database server using the provided settings. 

我的代碼如下

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class CI_DB_driver { 

    var $username; 
    var $password; 
    var $hostname; 
    var $database; 
    var $dbdriver  = 'mysql'; 
    var $dbprefix  = ''; 
    var $char_set  = 'utf8'; 
    var $dbcollat  = 'utf8_general_ci'; 
    var $autoinit  = TRUE; // Whether to automatically initialize the DB 
    var $swap_pre  = ''; 
    var $port   = ''; 
    var $pconnect  = FALSE; 
    var $conn_id  = FALSE; 
    var $result_id  = FALSE; 
    var $db_debug  = FALSE; 
    var $benchmark  = 0; 
    var $query_count = 0; 
    var $bind_marker = '?'; 
    var $save_queries = TRUE; 
    var $queries  = array(); 
    var $query_times = array(); 
    var $data_cache  = array(); 
    var $trans_enabled = TRUE; 
    var $trans_strict = TRUE; 
    var $_trans_depth = 0; 
    var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur 
    var $cache_on  = FALSE; 
    var $cachedir  = ''; 
    var $cache_autodel = FALSE; 
    var $CACHE; // The cache class object 

    // Private variables 
    var $_protect_identifiers = TRUE; 
    var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped 

    // These are use with Oracle 
    var $stmt_id; 
    var $curs_id; 
    var $limit_used; 



    /** 
    * Constructor. Accepts one parameter containing the database 
    * connection settings. 
    * 
    * @param array 
    */ 
    function __construct($params) 
    { 
     if (is_array($params)) 
     { 
      foreach ($params as $key => $val) 
      { 
       $this->$key = $val; 
      } 
     } 

     log_message('debug', 'Database Driver Class Initialized'); 
    } 

    // -------------------------------------------------------------------- 

    /** 
    * Initialize Database Settings 
    * 
    * @access private Called by the constructor 
    * @param mixed 
    * @return void 
    */ 
    function initialize() 
    { 
     // If an existing connection resource is available 
     // there is no need to connect and select the database 
     if (is_resource($this->conn_id) OR is_object($this->conn_id)) 
     { 
      return TRUE; 
     } 

     // ---------------------------------------------------------------- 

     // Connect to the database and set the connection ID 
     $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); 

     // No connection resource? Throw an error 
     if (! $this->conn_id) 
     { 
      log_message('error', 'Unable to connect to the database'); 

      if ($this->db_debug) 
      { 
       $this->display_error('db_unable_to_connect'); 
      } 
      return FALSE; 
     } 

     // ---------------------------------------------------------------- 

     // Select the DB... assuming a database name is specified in the config file 
     if ($this->database != '') 
     { 
      if (! $this->db_select()) 
      { 
       log_message('error', 'Unable to select database: '.$this->database); 

       if ($this->db_debug) 
       { 
        $this->display_error('db_unable_to_select', $this->database); 
       } 
       return FALSE; 
      } 
      else 
      { 
       // We've selected the DB. Now we set the character set 
       if (! $this->db_set_charset($this->char_set, $this->dbcollat)) 
       { 
        return FALSE; 
       } 

       return TRUE; 
      } 
     } 

     return TRUE; 
    } 
+0

並且你在你的'application \ config \ database.php'文件中配置了你的數據庫憑證嗎? – 2015-03-31 11:57:02

+0

是我配置文件 – 2015-03-31 12:24:33

+0

你想*處理或修復*這個錯誤?錯誤處理本身是一個非常廣泛的主題;)在錯誤處理的情況下,如果它暫時不可用,應該嘗試重新連接到數據庫。 – Alexander 2015-03-31 13:22:48

回答

1
var $username; 
    var $password; 
    var $hostname; 
    var $database; 

這些值是空白給予有效值上述變量

+0

我傳遞值,但它也給錯誤。 – 2015-03-31 12:02:53

+0

最新錯誤... – 2015-03-31 12:06:06

+0

發生同樣的錯誤.. – 2015-03-31 12:28:50

0

試試這個,

$param = array('username' => 'root', 'password' => 'password', 'hostname' => 'localhost', 'database' => 'DBNAME'); 

$db = new CI_DB_driver($param); 

$參數數組都所需的細節將用於連接數據庫。