2017-09-14 67 views
1

晚上好。即时通讯新手在代码点燃框架。我想问一下如何用代码点火器调用特定的数据。我的下面的代码打算从我的数据库中调用图片的名字。如何使用代码点火器调用数据库中的特定数据?

此我的模型

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" > 

上面的代码显示错误:数组到字符串的转换。我该如何解决它?感谢

回答

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了解更多信息

+0

如果他有超过一排? – chad

+1

OP正在使用'$ this-> db-> limit(1);',所以OP期待单行数据回报:) –

+0

没有注意到!好答案! – chad

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']因为对象之后,你assinging $data['profile']='profile';所以它给错误

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" > 
相关问题