2012-02-02 37 views
0

我的模型中有两种方法。一种是选择数据并将其返回给控制器(公共),另一种方法(私有)从数据库中选择数据并将其返回给公共方法。CodeIgniter从数据库中选择时未定义的索引

这是我的公开方法;

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()方法的返回行。

任何人都可以请帮我一下吗? 在此先感谢。

+1

定义$结果[ 'config_id']?在$ result = $ query-> row_array();之后尝试print_r($ result); – IsisCode 2012-02-02 13:46:03

+0

该数组是空的。问题是我不得不在我的查询中将1更改为'1'。 – Roel 2012-02-02 14:00:37

回答

2

试试这个

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