Invalid parameter number given with pdo's prepared statement

淺唱寂寞╮ 提交于 2019-12-13 08:17:20

问题


Hello I have made a query to select certain values from the database. Unfortunately I am getting this error message: Invalid parameter number

The code is as followes

$vehicles = DB::select( DB::raw("
        SELECT v.id, v.brand, v.type, v.description, v.airco, v.seats, v.hourly_rent 
        FROM vehicle as v 
        WHERE v.id 
        NOT IN((SELECT v.id FROM vehicle as v INNER JOIN reservation as r on r.`vehicle_id` = v.id WHERE r.status_id in(3,4,5) 
        AND (
                (
                    (:start >= r.startdate AND :eind <= r.enddate )
                    OR
                    (:start <= r.startdate AND :eind >= r.enddate ) 
                ) 
                OR
                (
                    (:start >= r.startdate AND :start <= r.enddate)
                    OR
                    (:eind >= r.startdate AND :eind <= r.enddate)
                )
            )
        GROUP BY v.id
        ))"), 
         array(
            'start' => $startdate, 
            'eind' => $enddate
         )
    );

回答1:


You can't use the same parameter name multiple times. Give each parameter its own name and bind exactly that number of parameters:

$vehicles = DB::select( DB::raw("
        SELECT v.id, v.brand, v.type, v.description, v.airco, v.seats, v.hourly_rent 
        FROM vehicle as v 
        WHERE v.id 
        NOT IN((SELECT v.id FROM vehicle as v INNER JOIN reservation as r on r.`vehicle_id` = v.id WHERE r.status_id in(3,4,5) 
        AND (
                (
                    (:starta >= r.startdate AND :einda <= r.enddate )
                    OR
                    (:startb <= r.startdate AND :eindb >= r.enddate ) 
                ) 
                OR
                (
                    (:startc >= r.startdate AND :startd <= r.enddate)
                    OR
                    (:eindc >= r.startdate AND :eindd <= r.enddate)
                )
            )
        GROUP BY v.id
        ))"), 
         array(
            'starta' => $startdate, 
            'einda' => $enddate,
            'startb' => $startdate, 
            'eindb' => $enddate,
            'startc' => $startdate, 
            'eindc' => $enddate,
            'startd' => $startdate, 
            'eindd' => $enddate
         )
    );



回答2:


You actually can. Just leave PDO in emulation mode.



来源:https://stackoverflow.com/questions/20670054/invalid-parameter-number-given-with-pdos-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!