发生数据库错误无法使用提供的设置连接到数据库服务器。文件名:core / Loader.php行号:338

时间:2015-10-16 10:19:14

标签: php mysql database codeigniter

我想从SERVER-ASERVER-B的数据库(在线,而不是xampp)进行连接。发生数据库错误。

  

无法使用提供的连接到数据库服务器   设置。文件名:core / Loader.php行号:338在database.php中

这是     代码

<?php $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'database_user';
    $db['default']['password'] = 'database_pass';
    $db['default']['database'] = 'database_name';
    $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] =
    ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] =
    TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir']
    = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE; $db['db2']['hostname'] = 'server
    name2'; $db['db2']['username'] = 'database_user2';
    $db['db2']['password'] = 'database_pass2'; $db['db2']['database'] =
    'database_name2'; $db['db2']['dbdriver'] = 'mysql';
    $db['db2']['dbprefix'] = ''; $db['db2']['pconnect'] = TRUE;
    $db['db2']['db_debug'] = TRUE; $db['db2']['cache_on'] = FALSE;
    $db['db2']['cachedir'] = ''; $db['db2']['char_set'] = 'utf8';
    $db['db2']['dbcollat'] = 'utf8_general_ci'; $db['db2']['swap_pre'] =
    ''; $db['db2']['autoinit'] = TRUE; $db['db2']['stricton'] = FALSE;
    ?> 

控制器

 <?php class testing extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $CI = &get_instance();
        $this->db2 = $CI->load->database('db2', TRUE);
        $ab = $this->db2->query('SELECT *
        from table_name')->result_array();
        echo $ab;
    }

} ?>

1 个答案:

答案 0 :(得分:0)

试试这个

<?php class testing extends CI_Controller {

//you don't need this if class testing does not override CI_Controller's constructor
//    function __construct() {
//        parent::__construct();
//    }

  public function index() {
      //getting the CI instance is not needed when extending CI_Controller
      //$CI = &get_instance();
      $db2 = $this->load->database('db2', TRUE);
      $ab = $db2->query('SELECT * from table_name')->result_array();
      echo $ab;
  }
} ?>