简体   繁体   English

PHP错误,未定义索引错误

[英]PHP Error, Undefined index error

In my codeigniter project I have this function in my model 在我的codeigniter项目中,我的模型中具有此功能

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 遇到PHP错误
Severity: Notice 严重程度:注意
Message: Undefined index: id , name and address 消息:未定义的索引:ID,名称和地址
Filename: models/form_model.php 文件名:models / form_model.php
line number 313 行号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 先感谢您

There are no rows in your table rtc_info . 您的表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 如果需要,不显示此通知,请在使用前通过isset()检查变量

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 如果这3个字段是可选的,那么您可以在没有else块的情况下使用

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 希望现在清除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM