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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度