Codeigniter mysql就像查询不起作用

时间:2017-01-20 10:25:03

标签: php mysql codeigniter

我有一个codeigniter查询,包含以下几行。

$userloc=$this->session->userdata('location');
$query=$this->db->query('select assettype,sum(status="In Stock")In_stock,sum(status = "Allocated")Allocated,sum(status = "Decommission")Decommission,sum(status = "In transit")In_Transit,location from assets group by assettype,location');

我想在$query location like $userloc中添加类似条件但是它没有按预期工作。

4 个答案:

答案 0 :(得分:1)

试试这个:

$userloc = $this->session->userdata('location');

$query = $this->db->query('select assettype, sum(status="In Stock") In_stock,
sum(status = "Allocated") Allocated,
sum(status = "Decommission") Decommission,
sum(status = "In transit") In_Transit,
location from assets where  location like "%'.$userloc.'%" group by assettype,location');

答案 1 :(得分:0)

尝试以下查询:

$query=$this->db->query('select assettype,sum(status="In Stock")In_stock,sum(status = "Allocated")Allocated,sum(status = "Decommission")Decommission,sum(status = "In transit")In_Transit,location from assets where location like %$userloc% group by assettype,location');

答案 2 :(得分:0)

尝试使用以下

$this->db->select('assettype,sum(status="In Stock")In_stock,sum(status =  "Allocated")Allocated,sum(status = "Decommission") Decommission,sum(status = "In transit")In_Transit,location');
$this->db->from('assets');
$this->db->like('location', $userloc);
$this->db->group_by('assettype,location');
$query = $this->db->get();

答案 3 :(得分:0)

使用以下查询

$userloc=$this->session->userdata('location');

$query=$this->db->query('select assettype,sum(status="In Stock")In_stock,sum(status = "Allocated")Allocated,sum(status = "Decommission")Decommission,sum(status = "In transit")In_Transit,location from assets where location like '%$userloc%' group by assettype,location');