使用单引号将变量包含在implode函数内

时间:2013-05-01 19:20:47

标签: php mysql

我正在尝试使用implode函数内的数组运行查询。

$dosages = implode(" AND CONCAT(prescription,'-',dosage) =",$doses);
$q=$this->db->query("SELECT account_num 
                     FROM ( 
                     SELECT account_num, CONCAT(prescription,'-',dosage) as script 
                     FROM ci_prescription_history 
                     )a 
                     WHERE script=".implode(' AND script=',$doses));

MySQL一直在'where子句'中说“未知列'Advair'”。我只是想知道是否可以用单引号将变量包装在implode函数中,所以当它被解析时它会像这样读出

SELECT account_num 
FROM ( SELECT account_num, CONCAT(prescription,'-',dosage) as script 
FROM ci_prescription_history 
)a 
WHERE script='Advair-230/21' AND script='Advair-250/50'

2 个答案:

答案 0 :(得分:1)

是的,您需要在查询和implode()中添加引号,如下所示:

$q=$this->db->query("SELECT account_num 
                 FROM ( 
                 SELECT account_num, CONCAT(prescription,'-',dosage) as script 
                 FROM ci_prescription_history 
                 )a 
                 WHERE script='" . implode("' AND script ='", $doses)) . "'";

答案 1 :(得分:0)

修改 这是我为解决它而做的事情

 foreach ($accounts as $account) {
     $q=$this->db->query("SELECT account_num 
     FROM ( 
     SELECT account_num, CONCAT(prescription,'-',dosage) as script 
     FROM ci_prescription_history 
     )a 
     WHERE script='".implode("' OR script ='",$doses)."' AND account_num=$account");
 }