Q:

PHP Error, Undefined index error

In my codeigniter project I have this function in my model
function get_rtc_info($id)
    {
        $query = $this->db->get_where('rtc_info', array('id' => $id));
        $query = $query->row_array();   
        $query = $query['id'].', '.$query['name'].', '.$query['address'];  //line# 313 
        return $query;
    }

I get the following error/warning when running the code

A PHP Error was encountered
  Severity: Notice
  Message: Undefined index: id , name and address
  Filename: models/form_model.php
  line number 313

I am not sure why I get this undefined index warnings. Can someone guide me on this error and how to resolve it. Thank you in advance

A:

There are no rows in your table rtc_info. This is why you are watching this kind of notice. If you want, not show this notice check your variable with isset() before using
function get_rtc_info($id)
{
    $query = $this->db->get_where('rtc_info',array('id' => $id));
    $query = $query->row_array(); 

    if(isset($query['id']) && isset($query['name']) && isset($query['address'])) {
     $query = $query['id'].', '.$query['name'].', '.$query['address'];  //line# 313 
     return $query;
}else{
   return 'no data';
  }
}

if these 3 fields are optional you may use following without else block
function get_rtc_info($id)
{
    $query = $this->db->get_where('rtc_info',array('id' => $id));
    $query = $query->row_array(); 

    if(isset($query['id'])){
        $row_id = $query['id'];
     }else{
        $row_id = 'no id';
     }

     if(isset($query['name'])){
        $name = $query['name'];
     }else{
        $name = 'no name';
     }

     if(isset($query['address'])){
        $address= $query['address'];
     }else{
        $address = 'no address';
     }

     $query = $row_id.', '.$name.', '.$address;  //line# 313 
     return $query;
}

hope now clear

