【问题标题】:how to call specific data in database using code igniter?如何使用codeigniter调用数据库中的特定数据?
【发布时间】:2017-09-14 07:56:57
【问题描述】:

晚上好,老师。我是代码点火器框架的新手。我想问一下如何使用代码点火器调用特定数据。我下面的代码打算从我的数据库中调用图片的名称。

这是我的模特

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

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

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

    需要将result()改为row()

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

    $ppicture;$ppicture-&gt;pict_name;

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

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

    【讨论】:

    • 如果他有不止一排怎么办?
    • OP 正在使用 $this-&gt;db-&gt;limit(1); ,因此 OP 期望返回单行数据 :)
    • 没注意到!好答案!
    • 它的工作,谢谢老师,你有很好的理解,注意到这是限制:D @JigarShah
    • 很高兴帮助你@emo,如果你觉得有用,别忘了点赞
    【解决方案2】:

    试试这个

    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();  
    }   
    

    之前$query = $this-&gt;db-&gt;get(); 正在返回对象,所以$data['ppicture'] 因为一个对象,然后你分配$data['profile']='profile'; 所以它给出错误

    【讨论】:

      【解决方案3】:

      这可能对你有帮助

      将您的模态更改为

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-20
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多