首先,我是框架的新手。使用CI数据库对象检查我正在尝试转录的查询。
$where = "(".$this->tbl.".invoiceNumber = '".substr($searchFor,3)."'
OR EXISTS (SELECT 1 FROM op_orders_products WHERE idProduct = ".(is_numeric(substr($searchFor,3)) ? substr($searchFor,3) : '-1')."
AND idOrder = ".$this->tbl.".id)
)";
我应该做一个单独的子查询吗?想把它全部集于一身。
这就是我的开始。我想确保变量是绑定的,而不是像原始查询中那样作为字符串传递。
$this->db->group_start()
->where($this->tbl.".invoiceNumber", substr($searchFor, 3))
->or_group_start()
// I'm missing this EXISTS select subquery
->group_end()
->group_end()
非常感谢你的帮助。
答案 0 :(得分:0)
在Codeigniter的查询生成器类中没有我所知道的EXISTS等效项。所以我只是这样写:
$this->db->group_start()
->where($where)
->group_end()
请参阅文档here
<强> binding: 强>
使用CodeIgniter,您可以将变量绑定到查询中,如下所示:
$sql = "SELECT * FROM table WHERE a= ? AND b= ?";
$query = $this->db->query($sql, array($a,$b));
这会将变量$ a和$ b绑定到查询字符串中的相应位置(?)。
<强> escaping 强>
$this->db->escape()
此函数转义字符串数据并在其周围添加单引号。