Laravel - 参数号无效:未定义参数

时间:2015-11-04 16:00:09

标签: php mysql laravel

在Laravel上,为什么我会收到错误

  

参数号无效:参数未定义

我已经包含了所有参数。

当我直接在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

3 个答案:

答案 0 :(得分:24)

您的查询失败,因为您正在重复使用查询中的参数。 Laravel使用PDO进行SQL查询。根据{{​​3}}:

  

除非启用仿真模式,否则不能在预准备语句中多次使用同名的命名参数标记。

因此即使它们具有相同的值,您也必须重命名这些参数。

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

答案 1 :(得分:3)

您正在laravel的查询构建器select()方法中插入所有SQL。您只需要使用其他方法:

$select = [
    'client_id',
    'start_date',
    'end_date',
    'first_name',
    'last_name',
    'phone',
    'postcode'
];

$results = \DB::table('hire')
        ->join('client', 'client.id', '=', 'hire.client_id')
        ->select($select)
        ->where('car_id', $car_id)
        ->whereBetween('start_date', [$start_date, $end_date])
        ->orWhereBetween('end_date', [$start_date, $end_date])
        ->get();

这些不是你的所有参数,但它应该让你开始。

如果您不想使用查询构建器,请尝试执行raw($expression)

$results = \DB::raw('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
    ]
);

答案 2 :(得分:0)

app / config / database.php

将此内容添加到mysql数组中

'options'=> extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                PDO::ATTR_EMULATE_PREPARES => true,
            ]) : [],