php - Laravel - 无效参数编号 : parameter was not defined

标签 php mysql laravel

在 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/

相关文章:

MySql server 5.0.96 有时挂起

php - 为我的学士学位构建 cms 并需要一些建议

php - Google 运算符的正则表达式

php - 导出 csv 之前设置数据库列别名

php - 在 Mac OSX 上非常慢的 laravel homestead/vagrant/virtualbox

validation - Laravel 所有输入字段都是必需的

laravel - 传递数组后在laravel中获取共享变量

javascript - 具有多个主题的 stomp.js 订阅阅读

php - 更正PHP中的编程错误

php - 将多个复选框值插入一个 MySQL 字段