从DB中选择时,CodeIgniter未定义索引


CodeIgniter undefined index when selecting from DB

我的模型中有两个方法。一种是选择数据并将其返回到控制器(公共),另一种方法(私有)是从数据库中选择数据并返回到公共方法。

这是我的公开方法;

public function get_template()
{
    // Get the active config.
    $config_id = $this->get_config();
    // Prepare the SQL query.
    $this->db->select('template');
    $this->db->from('tms_config');
    $this->db->where('config_id',$config_id);
    $this->db->limit(1);
    // Execute the query.
    $query = $this->db->get();
    // Get the result.
    $result = $query->row_array();
    // Make sure a template is set.
    if ( ! empty($result['template']))
    {
        return $result['template'];
    }
    else
    {
        // Return the default template.
        return DEFAULT_TEMPLATE;
    }
}

然后是私有方法。

private function get_config()
{
    // Prepare the SQL query.
    $this->db->select('config_id');
    $this->db->from('tms_config');
    $this->db->where('active',1);
    $this->db->limit(1);
    // Execute the query.
    $query = $this->db->get();
    // Get the result.
    $result = $query->row_array();
    // Return the configuration ID.
    return $result['config_id'];
}

显然出了问题,因为公共方法中的$config_id为空。出现此错误;

A PHP Error was encountered
Severity: Notice
Message: Undefined index: config_id
Filename: models/tms_config.php
Line Number: 140

第140行是get_config()方法的返回行。

有人能帮我解决这个问题吗?提前谢谢。

尝试这个

$result = $query->result_array();
return $result[0]['config_id'];