doushi3322
2018-06-11 08:12
浏览 97
已采纳

如何从codeigniter控制器获取id

I have written a query in Codeigniter controller but i am not sure how to use the variable from the database.

Controller:

 $query = $this->partner_vs_skill_model->where('partner_id',$id )
                                       ->like('skill_id', $value)->get_all();    
 $row = count($query);

What I expect is to use the code like below

 echo $query->id;

Database table (partner_vs_skill_model):

id | partner_id | skill_id 
1  |  2         | 3 
2  |  5         | 4
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglieshe4692 2018-06-11 08:20
    已采纳

    Hope this will help you :

    For One row use row(), like :

    $query = $this->partner_vs_skill_model->where('partner_id',$id )
                                          ->like('skill_id', $value)->row();
    echo $query->id;
    echo $query->skill_id;
    

    For multiple rows use result() instead, like :

    $query = $this->partner_vs_skill_model->where('partner_id',$id )
                                          ->like('skill_id', $value)->result();
    

    Access it like this :

    foreach($query as $row)
    {
      echo $row->id;
      echo $row->skill_id;
    }
    

    for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

    打赏 评论
  • douchao9899 2018-06-11 08:22

    you are getting an array of results to $query variable. what I am suggesting is to use below kind of code snippet to loop through the result and get the skill_tag variable

        foreach ($query->result() as $row) {
        echo $row->skill_tag
    }
    
    打赏 评论
  • dongxiangqian1855 2018-06-11 08:31

    In example: // controller-part:

     $query = $this->partner_vs_skill_model->get_partner_skill();
     echo $query;
    

    // model"

    <?php
          class partner_vs_sill_model extends CI_Model{
           public function get_partner_skill(){
            $result = $this->db->get('tb_name')->row_array();
            return $result
             }
          }
    
    打赏 评论

相关推荐 更多相似问题