Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Invalid parameter number: parameter was not defined

Tags:

php

mysql

laravel

On Laravel, why do I get an error

Invalid parameter number: parameter was not defined

I have included all parameters.

When I tested directly on PHPMyAdmin, it work fine.

Code:

$results = \DB::select('SELECT client_id,
                               date_format(start_date,"%d/%m/%Y") as start_date,
                               date_format(end_date,"%d/%m/%Y") as end_date,
                               first_name, last_name, phone, postcode
                            FROM hire INNER JOIN client ON client.id = hire.client_id
                            where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate <= start_date and end_date <= :edate)) AND car_id = :car_id', [
        'sdate'  => $start_date,
        'edate'  => $end_date,
        'car_id' => $car_id
    ]
);

Variable example:

$start_date = $inputs['start_date'];  //2015-10-27
$end_date = $inputs['end_date'];     //2015-10-27
$car_id = $inputs['car_id'];         //5
like image 459
I'll-Be-Back Avatar asked Nov 04 '15 16:11

I'll-Be-Back


1 Answers

Your query is failing because you are reusing parameters in your query. Laravel uses PDO for its SQL queries. According to the docs:

You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

So even though they have the same value, you will have to rename those parameters.

$results = \DB::select('SELECT client_id,
                           date_format(start_date,"%d/%m/%Y") as start_date,
                           date_format(end_date,"%d/%m/%Y") as end_date,
                           first_name, last_name, phone, postcode
                        FROM hire INNER JOIN client ON client.id = hire.client_id
                        where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate2 <= start_date and end_date <= :edate2)) AND car_id = :car_id', [
    'sdate'  => $start_date,
    'sdate2'  => $start_date,
    'edate'  => $end_date,
    'edate2'  => $end_date,
    'car_id' => $car_id
]
);
like image 164
Andy Noelker Avatar answered Sep 27 '22 23:09

Andy Noelker