【问题标题】:Get The Value of Single Result Array in CI Model获取 CI 模型中单个结果数组的值
【发布时间】:2023-04-05 19:49:05
【问题描述】:

我的模型上有这个功能。

function agent_claim($agentcode,$bankname, $accowner, $accnumber){
    $result= $this->db->query("INSERT INTO account (bank, number,name) VALUES ('$bankname', '$accnumber', '$accowner');");
    $accid= $this->db->query("SELECT LAST_INSERT_ID();")->result();
    $result = $this->agent_account($agentcode, $accid);
    return result;
}

此查询的返回值必须是单个值。

$accid= $this->db->query("SELECT LAST_INSERT_ID();")->result();

我不能用这种语法插入

$result = $this->agent_account($agentcode, $accid);

因为返回值不是数字而是数组。 如何获取 $accid 的值?

【问题讨论】:

  • $this->db->query("SELECT LAST_INSERT_ID();")->row();在这里尝试 row() 而不是 result()。你应该使用 $this->db->insert_id() 来获取最后一个插入 ID。也使用活动记录进行插入。

标签: arrays codeigniter model


【解决方案1】:

您应该使用如下的活动记录。 关于Active Records的参考

function agent_claim($agentcode,$bankname, $accowner, $accnumber){
        $data = array(
         'bank' => $bankname,
         'number' => $accnumber ,
         'name' => $accowner
        );
    //insert data to acccount table
        $this->db->insert('account', $data); 
    //get your last insert id
        $accid=  $this->db->insert_id();

        $result = $this->agent_account($agentcode, $accid); //this call your other function in model
        return $result;
    }

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2019-01-09
    • 2013-05-19
    • 1970-01-01
    相关资源
    最近更新 更多