在SQL查询Codeigniter中插入PHP变量

时间:2017-06-06 04:32:16

标签: php mysql codeigniter

我使用Codeigniter来使用一些查询,到目前为止这是我的代码。我的代码是否正确?

$this->db->select("(SELECT SUM(grand_total) FROM Sales_order WHERE member = '2') - (SELECT SUM(amount) FROM payment WHERE member_id = '2')  AS total_sales");

以及如何使用此变量更改数字
  $this->session->userdata('ID')

2 个答案:

答案 0 :(得分:0)

试试这个

$this->db->select("(SELECT SUM(grand_total) FROM Sales_order WHERE member = '".$this->session->userdata('ID')."') - (SELECT SUM(amount) FROM payment WHERE member_id = '".$this->session->userdata('ID')."')  AS total_sales"');

答案 1 :(得分:0)

您可以使用下面的Query Bindings自动转义它:

$sql = "(SELECT SUM(grand_total) FROM Sales_order WHERE member = '?') - (SELECT SUM(amount) FROM payment WHERE member_id = '?')  AS total_sales";
$this->db->query($sql, array($this->session->userdata('ID'),$this->session->userdata('ID')));