【问题标题】:How to use multiple placeholders (?) in raw Laravel query with in closure如何在关闭的原始 Laravel 查询中使用多个占位符(?)
【发布时间】:2016-09-20 09:53:24
【问题描述】:

这是我的代码...

//Database:: find matching results in feeds table
$users_size = $this->holders($this->followed_users_id);
$slugs_size = $this->holders($this->followed_slugs_id);

$match_query = "select * from feeds " .
    "WHERE (user_id IN ($users_size)) " .
    "OR (target_id IN ($slugs_size) AND feedable_type = 'review') " .
    "ORDER BY created_at DESC;";

    $result = DB::select($match_query, $this->followed_users_id, $this->followed_slugs_id);

不用担心 (?) 占位符,因为它们是根据要求动态生成的,并且 $this->followed_users_id$this->followed_slugs_id 是数组

结果是这样的

Connection.php 第 655 行中的 QueryException: SQLSTATE[HY093]: 无效的参数号(SQL: select * from feeds WHERE (user_id IN (1,17)) OR (target_id IN (?,?) AND feedable_type = 'review') ORDER BY created_at DESC;)

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    我认为绑定必须在一个数组中,并作为select 方法中的第二个参数

    $bindings = array_merge($this->followed_users_id, $this->followed_slugs_id);
    
    $result = DB::select($match_query, $bindings);
    

    【讨论】:

    • 是的,得到了​​解决方案...第三个参数不应该在那里
    • $consolidatedArray = array_merge($this->followed_users_id, $this->followed_slugs_id); $results = DB::select($match_query, $consolidatedArray);
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2016-08-27
    • 2021-12-05
    • 2018-12-18
    • 2015-02-25
    • 2022-06-27
    • 2021-06-24
    • 2019-07-13
    相关资源
    最近更新 更多