0

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

9
  • 1
    After $quary=$quary->row_array(); put die(var_dump($quary)); to see what items it contains. Commented Jun 8, 2015 at 16:27
  • You're probably calling that function with an $id that doesn't exist in the database, or your query is wrong. I'm not familiar with Codeigniter but you should find a way to check if the query returns results. If there no result, don't try to use them. Commented Jun 8, 2015 at 16:28
  • Check you $quary before concate , print_r($quary) Commented Jun 8, 2015 at 16:29
  • Thank you for the quick response! @ castis The result of die(var_dump($quary)); is array(0) { } . @caCtus23 I have the fields in the database, probably my query may be wrong, but It was working some hours ago. Commented Jun 8, 2015 at 16:32
  • @BeingSunny I get this message when using print_r($quary) CI_DB_mysql_result Object ( [conn_id] => Resource id #35 [result_id] => Resource id #47 [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => ) Commented Jun 8, 2015 at 16:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I added ur code snippet in my project, it works but the else block gets executed. Maybe the mistake is with my quary, since my database table has rows of data in it.
else block will be executed until your table contains no rows(and all 3 filed must have data id, name, address)
Thanks now the problem is solved, there was no data in the other table not the rtc table. Thank you for the help. Appreciate it!
@karma answer more explained, see now. If still confusing let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.