0%

PHP使用Heredoc拼接SQL等长字符串

使用Heredoc 结构来拼字符串

注意:在<<<该运算符之后要提供一个标识符(如SQL),然后换行。接下来是字符串 string 本身,最后要用前面定义的标识符(SQL)作为结束标志。

结束时所引用的标识符必须在该行的第一列,并且这行除了可能有一个分号(;)外,绝对不能包含其它字符。

很有代表性的示例:

public function test(){
$user= $this->session->userdata('user_sess');
//拼sql
$sql = <<<SQL
SELECT count(DISTINCT proteam.product_id) AS nums
FROM group_product pro
INNER JOIN group_product_team proteam ON pro.id = proteam.product_id AND proteam.caissa_op = '{$user['userCode']}' AND proteam.caissa_op_department = '{$user['deptCode']}'
WHERE pro.del_flag = 1
AND (
(proteam.STATUS=1/*AND proteam.start_date>='".$date."'*/)
OR (pro.status = '1' AND pro.caissa_op_claim = '{$user['userCode']}')
)
SQL;
$this->user_model->query($sql);
}

请我喝杯咖啡吧 Coffee time !