1

看着这个错误,我已经把头发拉出来太久了。我正在使用 Codeigniter 2 并创建了 MY_Controller 类来将一些设置加载到我的会话数据中。

您可以在下面看到:

class MY_Controller extends CI_Controller {  
    protected $ajax = 0;

    public function __construct() {
        parent::__construct();            
        $this->load->model('settings_model');

        //is the session user agent set
        if ($this->session->userdata('browser') == false)
        {
            $this->load->library('user_agent');
            $this->session->set_userdata(array(
                'browser' => $this->agent->browser(),
                'browser_version' => $this->agent->version(),
                'is_mobile' => $this->agent->is_mobile() ? 1 : 0
            ));                                                          
        }

        //is the settings loaded
        if ($this->session->userdata('league_name') == false)
        {                                       
            $this->Settings_model->get_general_settings();
        }

        //get the menu if we need to
        //if ($this->session->userdata('menu') == false)
            //$this->Settings_model->get_menu_items();  

        //set the main part of the title
        $this->set_title($this->session->userdata('league_name'));

        //get that referring url
        $this->session->set_userdata('refered_from', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : base_url()); 

        //ajax request
        $this->ajax = isset($_GET['ajax']) ? 1 : 0;
    }
}

我遇到问题的地方是我不断收到此错误:

Fatal error: Call to a member function get_general_settings() on a non-object in C:\xampp\htdocs\osmdev\application\controllers\welcome.php on line 12

这是我正在加载的 Settings_model.php 文件,是的,它位于我的 application/models 文件夹中:

class Settings_model extends CI_Model 
{                        
    // Call the Model constructor
    function __construct() {         
        parent::__construct();
    }

    //get the general settings
    function get_general_settings() {
        $query = "SELECT setting_id, variable, value FROM settings WHERE non_session = 0";
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));    
    }

    //get all the settings we have
    function get_all_settings() {        
        $query = "SELECT setting_id, variable, value FROM settings";
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));
    }

    //get a specfic setting variable
    function get_specific_setting($var) {           
        $query = "SELECT setting_id, variable, value FROM settings WHERE variable = '".$var;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));   
    }

    //get a specific type of setting
    function get_type_setting($type) {
        $query = "SELECT setting_id, variable, value FROM settings WHERE action = '".$type;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)
            $this->session->set_userdata($row['variable'], stripslashes($row['value']));    
    }

    //get all the menu items
    function get_menu_items($type=0) {
        $query = "SELECT menu_id, title, menu_url, parent_id, level, function_used, perm_id, icon FROM menu WHERE active = 1 AND is_admin_menu = '".$type;
        $result = $this->db->query($query);

        foreach ($result->result_array() as $row)  
            $items[$row['menu_id']] = $row;  

        $this->session->set_userdata('menu', $items);
    }  
}

我正在尝试调用 get_general_settings 函数。谁能看到我做错了什么?

4

2 回答 2

1

您可以尝试将模型设置为将 $row 存储到数组中,然后返回该数组。

您的型号:

function get_general_settings() {
    $rows = array();
    $query = "SELECT setting_id, variable, value FROM settings WHERE non_session = 0";
    $result = $this->db->query($query);

    foreach ($result->result_array() as $row)
        $variable = $row['variable'];
        $value = stripslashes($row['value']);
        $rows[] = $variable[$value];

    return $rows[]

你的控制器:

//is the settings loaded
    if ($this->session->userdata('league_name') == false)
    {                                       
        //set userdata to returned $rows[]
        $this->session->set_userdata($this->Settings_model->get_general_settings());
    }
//echo last_query to test in mysql
$this->db->last_query();

看看这是否有助于解决您的问题。我会 print_r($this->Settings_model->get_general_settings()); 只是看看是否有任何东西放在数组中。

然后,如果那里什么都没有,回显 last_query 以查看它要求 MySQL 做什么,然后在 mysql 中运行返回的查询,看看是否至少得到一行。

于 2013-11-08T19:01:47.940 回答
0

当您的数据库用户对您尝试查询的表没有选择权限时,可能会发生此错误。这没有意义,但它时常发生。

于 2014-05-08T12:20:08.163 回答