【问题标题】:Illuminate \ Database \ QueryException (HY093) SQLSTATE[HY093]: Invalid parameter numberIlluminate\Database\QueryException (HY093) SQLSTATE[HY093]: 参数号无效
【发布时间】:2019-09-28 10:07:27
【问题描述】:

我正在尝试从所有帖子中选择user_id,其中id 是while 循环中的当前索引,其vote1,并将其转换为数字数组。

但是,它一直给我这个错误:

Illuminate \ Database \ QueryException (HY093)
SQLSTATE[HY093]: Invalid parameter number (SQL: select `user_id` from `laravellikecomment_likes` where (`item_id` = 1 and `vote` = ?))

我不知道现在该做什么。这是我的代码(部分):

$db='laravellikecomment_likes';


  $allposts= DB::table($db)->where('vote','!=',0)->get()->pluck('user_id');
  $allposts = $allposts->toArray();

  $tn=count($allposts);
  $ai=0;
  $user=Auth::id();

  while ($ai <= $tn) {
     $recclist=array();
     $current=array_keys($allposts,$ai);
     $id=1;
     $wl=DB::table($db)->where(function ($query) use ($current, $id) {
        $query->where('item_id', '=', $current);
        $query->where('vote','=',$id);
    })->pluck('user_id');

【问题讨论】:

  • 您要解决的总体问题是什么?也许只需 1 个查询即可完成 - 请您阐明总体目标吗?
  • @party-ring 对不起....我不明白
  • 你能把你的问题解释清楚吗
  • @party-ring 我是 laravel 的新手,我正在尝试选择 id 为 while 循环中当前索引且投票为 1 的所有帖子的 user_id 并将其转换为数字数组.创建推荐引擎。我正在使用上面发布的查询,但帽子不起作用。请问正确的方法是什么?
  • 为什么要使用while循环?数值数组应该包含什么?

标签: sql database laravel eloquent


【解决方案1】:

抛出的错误与您的查询有关

$wl=DB::table($db)->where(function ($query) use ($current, $id) {
    $query->where('item_id', '=', $current); # This line is the culprit
    $query->where('vote','=',$id);
})->pluck('user_id');

抛出的错误,SQLSTATE[HY093]: Invalid Parameter number 提示参数错误。在这种情况下,您尝试使用array,而Query Builder 需要integerstring

如果要使用数组,请使用whereIn 而不是where,如下所示:

$wl=DB::table($db)->where(function ($query) use ($current, $id) {
    $query->whereIn('item_id', $current); # Use whereIn to deal with arrays
    $query->where('vote', '=', $id);
})->pluck('user_id');

【讨论】:

    猜你喜欢
    • 2023-01-09
    • 2016-03-04
    • 2013-08-04
    • 2016-12-04
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多