在编码器中从数据库中获取值


Fetching a value from database in codeigniter

我试图从数据库中获取一些值,我得到这个错误

遇到PHP错误
严重性:注意
Message: Undefined index: name
文件名:视图/pro_view.php
行号:12

我的控制器:

$this->load->model('profile_model');
$data['profile_data'] = $this->profile_model->get_records();
$this->load->view('pro_view', $data);

模型:

function get_records()
{
    $r = $this->uri->segment(3);
    $query = $this->db->get_where('profile', array('id' => $r));
    return $query->result();
}

视图:

<input type="text" name="name" value="<?php echo $profile_data["name"]  ?>" />

如果我是var_dump(($profile_data);,我将得到数组中的所有内容

我读了其他类似的问题和他们的答案,没有运气

如果您需要获取单行,则使用

$query = $this->db->get_where('profile', array('id' => $r));
$ret = $query->row();
return $ret->name;
控制器

$data['name'] = $this->profile_model->get_records();
$this->load->view('pro_view', $data);
视图

<input type="text" name="name" value="<?php echo $name;?>" />

对于你现在的代码,你需要使用foreach loop

 foreach (profile_data as $row)
   { ?>
<input type="text" name="name" value="<?php echo $row->name;?>" />
  <?php }?>

结果()

这个方法返回一个对象数组的查询结果,或者一个失败时为空数组。通常你会在foreach循环中使用

控制器

    $this->load->model('profile_model');
    $data['profile_data'] = $this->profile_model->get_records();
    $this->load->view('pro_view', $data);

$r = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('profile');
$this->db->where('id',$r);
$q = $this->db->get();
if($q->num_rows() > 0){
$row = $q->row();
return $row->name;
}
return false;
<<p> 视图/strong>
<input type="text" name="name" value="<?php echo $name;?>" />