Laravel-无效的参数编号:未定义参数 [英] Laravel - Invalid parameter number: parameter was not defined

查看:589
本文介绍了Laravel-无效的参数编号:未定义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel上,为什么会出现错误

On Laravel, why do I get an error

无效的参数编号:未定义参数

Invalid parameter number: parameter was not defined

我已包含所有参数.

当我直接在PHPMyAdmin上进行测试时,它工作正常.

When I tested directly on PHPMyAdmin, it work fine.

代码:

$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查询.根据文档:

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
]
);

这篇关于Laravel-无效的参数编号:未定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆