CodeIgniter - return only one row?

后端 未结 10 1350
鱼传尺愫
鱼传尺愫 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 03:13

    class receipt_model extends CI_Model {

       public function index(){
    
          $this->db->select('*');
    
          $this->db->from('donor_details');
    
          $this->db->order_by('donor_id','desc');
    
          $query=$this->db->get();
    
          $row=$query->row();
    
          return $row;
     }
    

    }

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

    To add on to what Alisson said you could check to see if a row is returned.

    // Query stuff ...
    $query = $this->db->get();
    
    if ($query->num_rows() > 0)
    {
        $row = $query->row(); 
        return $row->campaign_id;
    }
    
    return null; // or whatever value you want to return for no rows found
    
    0 讨论(0)
  • 2020-12-01 03:18

    You've just answered your own question :) You can do something like this:

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

    You can read more about it here: http://www.codeigniter.com/user_guide/database/results.html

    0 讨论(0)
  • 2020-12-01 03:18
    $this->db->get()->row()->campaign_id;
    
    0 讨论(0)
提交回复
热议问题