【问题标题】:Using Multiple in() condition in PDO query using php在使用 php 的 PDO 查询中使用 Multiple in() 条件
【发布时间】: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)。您需要使用其中之一。

标签: php pdo


【解决方案1】:

警告实际上告诉您出了什么问题。您生成的 SQL 字符串既有位置参数(即?)也有命名参数(例如:todate:fromdate)。您需要在 SQL 中使用其中之一,而不是两者。

解决此问题的一种方法是将您的 all 或 ? 用法转换为更复杂的命名参数。

例如,你的这部分代码:

if($rows['notification_counselor']!='0')
{
    $counselor=str_repeat('?,', count(explode(",",$rows['notification_counselor'])) - 1) . '?';
    $conditions.=" AND (st.counselorname IN($counselor))";
}

可以这样改写:


if ($rows['notification_counselor'] != '0') {
    $values = explode(',', $rows['notification_counselor']);
    $placeholders = [];
    foreach ($values as $key => $value) {
        $placeholders[] = ':notification_counselor_' . $key;
        $find_data[':notification_counselor_' . $key] = $value;
    }
    $conditions .= ' AND (st.counselorname IN(' . implode(', ', $placeholders) . '))';
}

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 2013-07-18
    • 2015-03-02
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2012-06-24
    相关资源
    最近更新 更多