【问题标题】:Laravel - Invalid parameter number: parameter was not definedLaravel - 无效的参数号:未定义参数
【发布时间】:2016-02-05 05:10:02
【问题描述】:

在 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

【问题讨论】:

  • Czan,您展示了如何为 3 个变量设置值?
  • @MateoBarahona 更新了我的问题。

标签: php mysql laravel


【解决方案1】:

您将所有 SQL 插入到 laravel 的查询构建器 select() 方法中。你只需要利用它的其他方法:

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

【讨论】:

  • 我明白,但为什么 select() 中的原始 SQL 会失败?
  • 您可以尝试使用selectRaw($select)(或raw($expression))来运行您的语句,但使用select() 只会将列添加到当前查询生成器。它不会处理您要运行的任何 SQL。更不用说您也没有使用 get() 方法要求任何结果。 github.com/laravel/framework/blob/5.1/src/Illuminate/Database/…
  • 虽然使用查询构建器或 Eloquent ORM 确实是首选,但将原始 SQL 添加到 DB 外观的 select 方法是完全有效的。 laravel.com/docs/5.1/database#running-queries
  • 不知道!感谢您清除此问题@Andy Noelker
【解决方案2】:

您的查询失败,因为您在查询中重复使用参数。 Laravel 使用 PDO 进行 SQL 查询。根据docs

您不能在准备好的语句中多次使用同名的命名参数标记,除非打开了仿真模式。

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

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

【讨论】:

【解决方案3】:

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,
            ]) : [],

【讨论】:

  • 想详细说明这到底是做什么的?
  • 这并没有解决或解决问题,至少不是在所有情况下,因为我在使用 PHP8 升级到新服务器后仍然遇到这个问题
猜你喜欢
  • 2017-12-13
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
相关资源
最近更新 更多