简体   繁体   English

无法连接到本地CodeIgniter数据库

[英]cannot connect to local CodeIgniter database

I'm pretty new to CodeIgniter and php. 我对CodeIgniter和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 我试图开始在CodeIgniter php网站上工作,但我不断得到:未找到类'DB'

I have an error on line 92 this is the line 我在第92行有错误,这是该行

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

Don't use this method. 不要使用此方法。 Go to your applications -> config -> database.php and enter in your database credentials there. 转到您的applications -> config -> database.php然后在其中输入数据库凭据。

Then head to applications -> config -> autoload.php and in the libraries array add database eg: 然后转到applications -> config -> autoload.php并在libraries数组中添加database例如:

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

(Should be around line 55). (应该在第55行附近)。

Then, you can use the database guide and Codeigniter's built in database helper to do what you want. 然后,您可以使用数据库指南和Codeigniter的内置数据库帮助程序来执行所需的操作。

You want to do all of these in your model . 您想在model完成所有这些操作。 Eg: 例如:

function madeup_stuff($id){
    $query = $this->db->get_where("table_name", array("id" => $id));
    $data = array();
    foreach ($query as $q){
        $data[] = $q;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM