CodeIgniter - return only one row?

后端 未结 10 1349
鱼传尺愫
鱼传尺愫 2020-12-01 02:43

At the moment if I am doing a query on the database that should only return one row, using:

...query stuff...
$query = $this->db->get();
$ret = $query-         


        
相关标签:
10条回答
  • 2020-12-01 02:57

    Change only in two line and you are getting actually what you want.

    $query = $this->db->get();
    $ret = $query->row();
    return $ret->campaign_id;
    

    try it.

    0 讨论(0)
  • 2020-12-01 02:58

    You can do like this

    $q  = $this->db->get()->row();
    
    return $q->campaign_id;
    

    Documentation : http://www.codeigniter.com/user_guide/database/results.html

    0 讨论(0)
  • 2020-12-01 03:01

    This is better way as it gives you result in a single line:

    $this->db->query("Your query")->row()->campaign_id;
    
    0 讨论(0)
  • 2020-12-01 03:01

    We can get a single using limit in query

    $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

     $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
    
    0 讨论(0)
  • 2020-12-01 03:08

    If you require to get only one record from database table using codeigniter query then you can do it using row(). we can easily return one row from database in codeigniter.

    $data = $this->db->get("items")->row();
    
    0 讨论(0)
  • 2020-12-01 03:10

    To make the code clear that you are intending to get the first row, CodeIgniter now allows you to use:

    if ($query->num_rows() > 0) {
        return $query->first_row();
    }
    

    To retrieve the first row.

    0 讨论(0)
提交回复
热议问题