duannan3959 2014-03-04 12:30
浏览 53
已采纳

无法连接到本地CodeIgniter数据库

I’m pretty new to CodeIgniter and php. I have this issue which I can’t figure out.

I am trying to start working on a CodeIgniter php site and I keep getting: Class 'DB' not found

I have an error on line 92 this is the line

try {
            DB::connect(DB_ADAPTER, array(
              'host'    => DB_HOST,
              'user'    => DB_USER,
              'pass'    => DB_PASS,
              'name'    => DB_NAME,
              'persist' => DB_PERSIST
            )); // connect
            if (defined('DB_CHARSET') && trim(DB_CHARSET)) {
              DB::execute("SET NAMES ?", DB_CHARSET);
            } // if
            DB::execute('ROLLBACK');
            DB::execute('UNLOCK TABLES');
            DB::execute('SET AUTOCOMMIT=1');

          } catch(Exception $e) {
            if (Env::isDebugging()) {
              Env::dumpError($e);
            } else {
              Logger::log($e, Logger::FATAL);
              Env::executeAction('error', 'db_connect');
            } // if
          } // try
  • 写回答

1条回答 默认 最新

  • drtoaamk20278 2014-03-04 12:36
    关注

    Don't use this method. Go to your applications -> config -> database.php and enter in your database credentials there.

    Then head to applications -> config -> autoload.php and in the libraries array add database eg:

    $autoload['libraries'] = array('database');
    

    (Should be around line 55).

    Then, you can use the database guide and Codeigniter's built in database helper to do what you want.

    You want to do all of these in your model. E.g:

    function madeup_stuff($id){
        $query = $this->db->get_where("table_name", array("id" => $id));
        $data = array();
        foreach ($query as $q){
            $data[] = $q;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?