【问题标题】:Fatal database error on insert() with CodeIgniter使用 CodeIgniter 的 insert() 发生致命数据库错误
【发布时间】:2015-09-22 09:11:05
【问题描述】:

我收到此错误,我不知道该怎么办:

遇到 PHP 错误
严重性:通知
消息:未定义属性:Welcome::$db
文件名:core/Model.php
行号:52
致命错误:在第 38 行的 C:\xampp\htdocs\site\application\models\account_model.php 中的 null 上调用成员函数 insert()

控制器:

public function register()//function register in the controller
{
        $this->load->model('account_model');
        $this->account_model->create_account(); 
}

型号:

function create_account()//function insert.
{
    $insertdata = array('id' =>'',
        'firstname' => $this->input->post('fname'),
        'username' => $this->input->post('email'),
        'username' => $this->input->post('username'),
        'password' => $this->input->post('pass'),
        'role' => '1',
        'status' => 'Active');

    $insert = $this->db->insert('account', $insertdata);
    return $insert; 
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您需要将数据库库加载到您的自动加载配置文件中,您已经这样做了吗?

    位于../application/config/autoload.php,搜索:

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

    并像这样添加数据库库:

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

    【讨论】:

    • 大帮助康斯坦丁。但是,在我更改它之后,又出现了另一个错误。您的 application/config/database.php 文件似乎没有包含有效的配置数组。
    • 对不起,我的错。这是错误。 --- 无法使用提供的设置连接到您的数据库服务器。文件名:C:\xampp\htdocs\site\system\database\DB_driver.php 行号:125
    【解决方案2】:

    config/autoload.php 中,加载database

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

    【讨论】:

    • 我收到了这个错误。您的 application/config/database.php 文件似乎没有包含有效的配置数组。
    • 对不起,我的错。这是错误。 --- 无法使用提供的设置连接到您的数据库服务器。文件名:C:\xampp\htdocs\site\system\database\DB_driver.php 行号:125
    • 检查您的database.php 并提供您的数据库详细信息
    • @adulla 感谢您的大力帮助。我已经弄清楚是什么问题了。直到下一次!祝你有美好的一天。
    • @Glenn 好的。享受编码:)