在 Laravel 上,为什么会出现错误
Invalid parameter number: parameter was not defined
我已经包含了所有参数。
当我直接在 PHPMyAdmin 上测试时,它工作正常。
代码:
$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
]
);
变量示例:
$start_date = $inputs['start_date']; //2015-10-27
$end_date = $inputs['end_date']; //2015-10-27
$car_id = $inputs['car_id']; //5
最佳答案
您的查询失败,因为您在查询中重复使用了参数。 Laravel 使用 PDO 进行 SQL 查询。根据docs :
You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.
因此即使它们具有相同的值,您也必须重命名这些参数。
$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
]
);
关于php - Laravel - 无效参数编号 : parameter was not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33526391/