Codeigniter this-> db->查询未识别的索引

时间:2017-07-04 20:43:13

标签: php mysql codeigniter

我正在尝试使用CodeIgniter执行以下查询,但我正在

  

未知的索引 - > row ['code'];

public function getSExtension($id) {
  $temp = array();
  $query = $this->db->query("SELECT extension_id FROM " . DB_PREFIX . "zipcode_shipping WHERE `zip_code` = '" . $id . "'");
  if($query->num_rows) {
    foreach ($query->rows as $key => $value) {
      $code = $this->db->query("SELECT code FROM " . DB_PREFIX . "extension WHERE `extension_id` = '" . $value['extension_id'] . "'")->row['code'];
      $this->language->load("shipping/".$code);
      $temp[] = $this->language->get('text_title');
    }
  }
  return $temp;
}

1 个答案:

答案 0 :(得分:0)

问题是您没有正确检索需要方法调用的结果集。如你所知,你试图使用不存在的db属性,即$query->rows应该是$query->row_array()

Documentation Here.

在下面的示例中,我使用result()row()代替任何“数组”方法。

试试这个。

$temp = array();
$query = $this->db->query("SELECT extension_id FROM ".DB_PREFIX."zipcode_shipping WHERE `zip_code` = '".$id."'");

if($query->num_rows() > 0)
{
    foreach($query->result() as $value)
    {
        $code = $this->db->query("SELECT code FROM ".DB_PREFIX."extension WHERE `extension_id` = '".$value->extension_id."'")->row()->code;
        $this->language->load("shipping/".$code);
        $temp[] = $this->language->get('text_title');
    }
}