我尝试使用子字符串从数据库中获取一串数据,但结果是'where子句'中的未知列'code'
示例我的字符串 RS0002030720160003
我想要 RS0002
这是我的控制器
public function report(){
$nomor = $this->input->post('nomor_reseller');
$report_data = $this->hasil_m->get_data($nomor);
if ($nomor == "" ) {
$this->session->set_flashdata('msg',
'<div class="alert alert-danger text-center">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Nomor atau bulan Harus di isi </div>');
redirect('admin/hasil');
} elseif ($report_data == NULL ) {
$this->session->set_flashdata('msg',
'<div class="alert alert-danger text-center">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
kosong </div>');
redirect('admin/hasil');
} else {
$data=array('title' =>'KOPKAR - Pelanggan',
'report_data' => $this->hasil_m->get_data($nomor),
'isi' =>'admin/hasil'
);
$this->load->view('dashboard/wrapper',$data);
}
}
我的观点
<table class="table table-hover table-striped">
<thead>
<tr>
<th><div align="center">No</th>
<th class="text-center">Produk</th>
<th class="text-center">Jumlah Produk Terjual</th>
<th class="text-center"></th>
<th class="text-center"></th>
<th class="text-center">Rp.</th>
<th class="text-center">Action</div></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($report_data as $row) {
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row->id_barcode; ?></td>
<td><?php echo $row->jumlah; ?></td>
<td class="fontCap text-center"><?php ?></td>
<td class="fontCap text-center"><?php ?></td>
<td><div align="center"></div></td>
<td><div align="center">
<a href="#" title="delete" onclick="return confirm('Anda yakin ingin menghapus produk dengan ID pada database?');" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i></a>
</div>
</td>
<?php
$no++;
}
?>
</tr>
</table>
这是我的模块
function get_data($nomor) {
$this->db->select('SUBSTRING(id_barcode, 1, 7) as code', false);
$this->db->from('tb_pelanggan');
$this->db->where('code',$nomor);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
答案 0 :(得分:1)
这几乎与PHP或CodeIgniter框架无关 - 这正是您用来访问数据库的问题,问题出在数据库级别......
WHERE
条件适用于列名,但不适用于您放入查询SELECT
部分的别名。
如果要与已选择的数据进行比较,则应使用HAVING
代替。
只需将您的where()
来电替换为相同的having()
,即可使用。
答案 1 :(得分:0)
尝试这样的事情......
$this->db->select('SUBSTRING(id_barcode, 1, 7) as code', false);
$this->db->where("(SUBSTRING(id_barcode, 1, 7) = '$nomor')");
$this->db->from('tb_pelanggan');
$A=$this->db->get();
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
您的查询正在执行此操作
SELECT SUBSTRING(id_barcode, 1, 5) as code FROM (tb_pelanggan) WHERE SUBSTRING(id_barcode, 1, 5) 'sadsa'
应该是这样的
SELECT SUBSTRING(id_barcode, 1, 7) as code FROM (tb_pelanggan) WHERE (SUBSTRING(id_barcode, 1, 7) = 'sadsa')