在Codeigniter中返回数据库结果时未定义的索引


Undefined index when returning database results in Codeigniter

我正试图将数据从Wordpress数据库转移到Codeigniter项目中。我有一个返回单个值的模型方法,但我一直得到以下结果:

消息:未定义的索引:元值

文件名:models/transfer_model.php

这是型号:

 public function get_meta($post_id, $key)
    {
        $this->db->select('meta_value');
        $this->db->from('wp_postmeta');
        $this->db->where('post_id', $post_id);
        $this->db->where('meta_key', $key);
        $this->db->limit(1);
        $query = $this->db->get();
        $row = $query->row_array();
        return $row['meta_value'];
    }

如果我print_r($row);,我得到以下内容:

Array
(
    [meta_value] => The Data
)

如何阻止它返回此错误?

如果有提取的记录,当没有通过查询提取的记录时,您将得到返回,那么将没有任何内容,这意味着您的行是空的,以避免这种情况

array_key_exists

public function get_meta($post_id, $key)
    {
        $this->db->select('meta_value');
        $this->db->from('wp_postmeta');
        $this->db->where('post_id', $post_id);
        $this->db->where('meta_key', $key);
        $this->db->limit(1);
        $query = $this->db->get();
        $row = $query->row_array();
        $return = '';
        if(key_exists('meta_value',$row))
         $return = $row['meta_value'];
        return $return;
    }

如果使用整数索引怎么办?

$row = $query->row_array();
return $row[0];