我试图从数据库中获取数据,方法是从一个表中获取一个数据行,并将这些值用作where条件。然后我尝试将json字符串传递给控制器类。但我够不着。我应该如何编写此代码这是我用来获取数据的代码。
$query = $this->db->query("SELECT * FROM todaywork WHERE idEmployee = $user ");
if ($query->num_rows() > 0)
{
$row = $query->row();
$from = $row->T_frange;
$to = $row->T_trange;
$note = $row->T_note;
}
$this->db->select("cus.idCustomer,cus.C_name,f.F_itemname,ctr.*");
//$whereCondition = array('cus.idCustomer' => $_POST['custId']);
$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}');
//filter where
$this->db->from('customer cus');
$this->db->join('contracts AS ctr', 'cus.idCustomer = ctr.idCustomer', 'INNER');
$this->db->join('facility AS f','ctr.idContracts = f.idContracts','INNER');
$query = $this->db->get();
return $query->result();
}
在之间使用此选项
$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);
替换此,
$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}');
有了这个,
$this->db->where('C_trnoofarr >', $from);
$this->db->where('C_trnoofarr <', $to);
不过,如果要在WHERE
子句中使用raw,还必须添加第三个参数。
$this->db->where('<where clause>', NULL, FALSE);
来自文档:
$this->db->where()接受可选的第三个参数。如果您将其设置为FALSE,CodeIgniter将不会尝试使用反标记来保护字段或表名。
在你的情况下,也许是这样,
$this->db->where('C_trnoofarr BETWEEN {$from} AND {$to}', NULL, FALSE);
$this->db->select('*');
$this->db->from('company');
$this->db->where("grade BETWEEN $first AND $second");
$this->db->get()->result();