【发布时间】:2019-12-17 07:18:18
【问题描述】:
我在 PDO Select 查询的 WHERE 子句中使用了多个 in() 条件。作为 in() 条件的值,我试图通过使用explode 语句将字符串转换为数组来使用数组。像这样,
$conditions="";
if($rows['notification_counselor']!='0')
{
$counselor=str_repeat('?,', count(explode(",",$rows['notification_counselor'])) - 1) . '?';
$conditions.=" AND (st.counselorname IN($counselor))";
}
if($rows['notification_source']!='0')
{
$source=str_repeat('?,', count(explode(",",$rows['notification_source'])) - 1) . '?';
$conditions.=" AND (st.source IN($source))";
}
if($rows['notification_type']!='0')
{
$type=str_repeat('?,', count(explode(",",$rows['notification_type'])) - 1) . '?';
$conditions.=" AND (st.type IN($type))";
}
if($rows['notification_program']!='0')
{
$program=str_repeat('?,', count(explode(",",$rows['notification_program'])) - 1) . '?';
$conditions.=" AND (st.program IN($program))";
}
这里$rows['notification_counselor']是字符串,所以我用explode函数把它改成数组。
但我只得到这个结果:
SELECT st.email FROM tbl_studentrecord st LEFT JOIN tbl_callrecord cr ON st.student_id_pk=cr.student_id_fk WHERE (DATE(st.createddate) >= :fromdate AND DATE(st.createddate) <= :todate) AND (st.counselorname IN(?,?,?))
出现此错误:
警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数号:混合命名参数和位置参数
其余代码如下,
$stmt = $crsmspdo->prepare("SELECT st.email FROM tbl_studentrecord st LEFT JOIN tbl_callrecord cr ON st.student_id_pk=cr.student_id_fk WHERE (DATE(st.createddate) >= :fromdate AND DATE(st.createddate) <= :todate) $conditions GROUP BY cr.student_id_fk");
$find_data=array('fromdate' =>$rows['notification_fdate'],'todate' =>$rows['notification_tdate']);
$stmt->execute($find_data);
但是,我希望实际结果是这样的,
SELECT st.email FROM tbl_studentrecord st LEFT JOIN tbl_callrecord cr ON st.student_id_pk=cr.student_id_fk WHERE (DATE(st.createddate) >= :fromdate AND DATE(st.createddate) <= :todate) AND (st.counselorname IN(5,6,7))
我不知道我在哪里犯了错误。请帮助我解决我的代码错误或提供任何替代想法来实现这一点..,
提前致谢!
【问题讨论】:
-
由于 :fromdate 和 :todate ,您会收到错误消息。使用 ?.
-
警告实际上告诉你出了什么问题。您生成的 SQL 字符串既有位置参数(即
?),也有命名参数(例如:todate、:fromdate)。您需要使用其中之一。