【问题标题】:Trying to fetch the last inserted record in php试图在php中获取最后插入的记录
【发布时间】:2020-09-14 06:23:59
【问题描述】:

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

代码是:

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

【问题讨论】:

  • primary 键列上使用order by desc

标签: php mysql codeigniter


【解决方案1】:

您在primary key 列上使用order by desc,如下所示

注意:如果主键是invoice_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'];
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多