简体   繁体   English

为什么会出现此未定义索引错误?

[英]Why am I getting this undefined index error?

I'm trying to list the indexes of an array, and it keeps throwing errors in my face. 我试图列出一个数组的索引,并且它总是在我面前抛出错误。

I am getting this error: 我收到此错误:

A PHP Error was encountered 遇到PHP错误
Severity: Notice 严重程度:注意
Message: Undefined index: sender 消息:未定义的索引:发件人

Now for some code details: My print line: 现在获取一些代码详细信息:我的打印行:

<div class="left"><label>Afsender: </label><p><?=$reservation['sender'] . ' (' .     $reservation['email'] . ')'?></p></div></pre>

My controller: (using phil sturgeon's template library for CodeIgniter) 我的控制器:(将phil sturgeon的模板库用于CodeIgniter)

public function readReservation($id)
{
    $data['reservation'] = $this->reservation_model->getReservation($id);
    $this->template->build('admin/readReservation_view', $data);
}

My Model: 我的模特:

public function getReservation($id) 
{
    $query = $this->db->where('id', $id)->get('ita_reservations');

    if ($query->num_rows > 0) 
    {
        return $query->result();
    }
    else
        return false;
}

My sql: 我的SQL:

CREATE TABLE IF NOT EXISTS `ita_reservations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sender` varchar(200) NOT NULL,
  `email` varchar(200) NOT NULL,
  `date` varchar(200) NOT NULL,
  `time` varchar(200) NOT NULL,
  `persons` varchar(200) NOT NULL,
  `phone` varchar(200) NOT NULL,
  `message` varchar(1000) NOT NULL,
  `time_received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `confirmed` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

Now what baffles me, is that the same code works elsewhere, but with a different variable name. 现在让我感到困惑的是,相同的代码在其他地方也可以使用,但是具有不同的变量名。 To increase my confusion, this is the var_dump from the line above my print: 为了增加我的困惑,这是我打印上方一行中的var_dump

array(1) { [0]=> object(stdClass)#42 (10) { ["id"]=> string(1) "1" ["sender"]=> string(7) "Kenneth" ["email"]=> string(17) "zungate@gmail.com" ["date"]=> string(10) "23/05/2013" ["time"]=> string(5) "18:00" ["persons"]=> string(1) "4" ["phone"]=> string(8) "30243681" ["message"]=> string(12) "Ja nemlig ja" ["time_received"]=> string(19) "0000-00-00 00:00:00" ["confirmed"]=> string(1) "0" } } array(1){[0] => object(stdClass)#42(10){[“ id”] => string(1)“ 1” [“ sender”] => string(7)“ Kenneth” [“电子邮件“] =>字符串(17)” zungate@gmail.com“ [”日期“] =>字符串(10)” 23/05/2013“ [”时间“] =>字符串(5)” 18:00“ [“ persons”] =>字符串(1)“ 4” [“电话”] =>字符串(8)“ 30243681” [“消息”] =>字符串(12)“ Ja nemlig ja” [“ time_received”] = > string(19)“ 0000-00-00 00:00:00” [“ confirmed”] => string(1)“ 0”}}

The sender index is clearly there, and the array is clearly loaded properly. 发件人索引显然在那里,并且该数组显然已正确加载。 (All indexes throw errors, not just sender). (所有索引都会引发错误,而不仅仅是发送方)。

What's going on, and what am I missing? 发生了什么,我想念什么? Yes, I know I should check for isset or empty , but it exists, and the page cannot be loaded if the index doesn't exist so checks are kindof moot. 是的,我知道我应该检查issetempty ,但是它存在,并且如果索引不存在,则无法加载该页面,因此检查是没有意义的。

You have an object inside an array (see your var_dump ), so to get the reservation sender you have to use the following: 您在数组中有一个对象(请参见var_dump ),因此要获取预留发送者,您必须使用以下命令:

$reservation[0]->sender

Or better yet, instead of returning the whole result set from your function, you should only return one row (assuming id's are unique, your query will return only one row anyway): 或者更好的是,不要从函数中返回整个结果集,而应该只返回一行(假设id是唯一的,无论如何查询将只返回一行):

public function getReservation($id) 
{
  $query = $this->db->where('id', $id)->get('ita_reservations');

  if ($query->num_rows > 0) 
  {
    return $query->row();
  }
  else
    return false;
}

With this you can use: 与此可以使用:

$reservation->sender

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

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