gpt4 book ai didi

php - 检查php mysql事件记录中是否不存在数据然后选择默认代码

转载 作者:行者123 更新时间:2023-11-29 17:58:22 27 4
gpt4 key购买 nike

这段代码为我获取数据

function get_mdr_master($kode) {
//$this->db->cache_on();
$this->db->select('mdr_debit, mdr_debit_npg, mdr_debit_pl');
$this->db->where('kode',$kode);
$query = $this->db->get('mapping_mcc_mdr');
//echo $query;die;
return $query->result();
}

但是如果数据($kode)不存在,我想指出$this->db->where('kode',$kode);选择我已经在数据库中准备好的值为 0000 的 $kode。

那么如何制作 if else 语句,如果数据库中不存在该数据,则选择 0000 作为默认 $kode?

最佳答案

使用 $kode 检查 where 是否有超过 0 行,如果没有,则执行相同的逻辑,但使用 where 0000:

function get_mdr_master($kode) {
$this->db->select('mdr_debit, mdr_debit_npg, mdr_debit_pl');
$this->db->where('kode', $kode);
$query = $this->db->get('mapping_mcc_mdr');
if ($query->num_rows() > 0) {
return $query->result();
} else {
$this->db->select('mdr_debit, mdr_debit_npg, mdr_debit_pl');
$this->db->where('kode', '0000');
$query = $this->db->get('mapping_mcc_mdr');
return $query->result();
}
}

或者(更漂亮):

function get_mdr_master($kode) {
$this->db->where('kode', $kode);
if ($this->db->count_all_results('mapping_mcc_mdr') < 1) {
$kode = '0000'; // override $kode
}
$this->db->select('mdr_debit, mdr_debit_npg, mdr_debit_pl');
$this->db->where('kode', $kode);
$query = $this->db->get('mapping_mcc_mdr');
return $query->result();
}

关于php - 检查php mysql事件记录中是否不存在数据然后选择默认代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48615440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com