如何使用代码点火器调用数据库中的特定数据?

时间:2017-09-14 07:56:58

标签: php codeigniter

晚安老师。我新手在代码点火器框架。我想询问如何使用代码点火器调用特定数据。我的代码用于从我的数据库中调用图片的名称。

这是我的模特

function profile_picture(){     
$username = $this->session->userdata('name');
$this->db->select('pict_name');
$this->db->from('picture');
$this->db->where('username="$username"');
$this->db->limit(1);
$query = $this->db->get();
return $query;  
}   

这是我的控制器

public function v_profile(){
    $data['ppicture'] = $this->m_profile->profile_picture()->result();
    $data['profile'] = "profile";
    $this->load->view('header',$data);
    $this->load->view('profile',$data);
    $this->load->view('footer',$data);
}

这是我的观点

<img src="<?php echo base_url()."assets/img/".$ppicture; ?>" class="img-rounded" >

上面的代码显示错误:数组到字符串的转换。我该怎么办呢?感谢

3 个答案:

答案 0 :(得分:2)

您正在访问数组,result()提供数组,

需要将result()更改为row()

$data['ppicture'] = $this->m_profile->profile_picture()->row();

$ppicture;$ppicture->pict_name;

<img src="<?php echo base_url()."assets/img/".$ppicture->pict_name; ?>" class="img-rounded" >

您可以参考documentation了解更多信息

答案 1 :(得分:0)

试试这个

function profile_picture(){     
$username = $this->session->userdata('name');
$this->db->select('pict_name');
$this->db->from('picture');
$this->db->where('username="$username"');
$this->db->limit(1);
$query = $this->db->get();
return $query->result_array();  
}   

Befor $query = $this->db->get();正在返回对象$data['ppicture']因为一个对象,之后你正在协助$data['profile']='profile';所以给出错误

答案 2 :(得分:0)

这可能对您有所帮助

将您的模态更改为

function profile_picture(){     
$username = $this->session->userdata('name');
$this->db->select('pict_name');
$this->db->from('picture');
$this->db->where('username="$username"');
$this->db->limit(1);
$query = $this->db->get();
return $query->result_array();  
}

在您的视图页面

<img src="<?php echo base_url(); ?>assets/img/<?php echo $ppicture[0]['table filed name for image'];?>" class="img-rounded" >
相关问题