这段代码为我获取数据
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?p>
最佳答案
使用 $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/