尝试在php中获取最后插入的记录

尝试在php中获取最后插入的记录,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在做“发票表”。它有“发票id”和“金额”字段。我需要获取最新插入的“发票id”的“金额”的最新值 代码是: $camount = $this->db->select_max('invoice_id')->get_where('invoice', array('invoice_id' => $this->input->post('invoice_id')))->row()->amount; 您可以在主键列中使用按des排序c,如下所示 注意:

我正在做“发票表”。它有“发票id”和“金额”字段。我需要获取最新插入的“发票id”的“金额”的最新值

代码是:

$camount = $this->db->select_max('invoice_id')->get_where('invoice', array('invoice_id' => $this->input->post('invoice_id')))->row()->amount;

您可以在
主键
列中使用
按des排序
c,如下所示

注意:如果主键是
invoice\u id
,那么为什么要设置where条件

// add your where conditions e.g: $this->db->where('Field / comparison', 'value');

        $this->db->order_by('invoice_id', 'desc');
        $invoice = $this->db->get('invoice')->row_array();
        if($invoice){
            $camount = $invoice['amount'];
        }

primary
键列上使用
order by desc