DB类的使用,sql问题

$connection=DB::connection (‘mysql’)->table (表名称)
问:下面原本的数据怎么防止注入,sql 可以执行成功
原本的 (可以执行成功,没什么问题,根据可能会出现 sql 注入)
主要解决 where in 绑定的参数过多执行不了的问题

if (count($values)>300) {
    $values=collect($values)->map(function($value){
        return "'".$value."'";
    })->toArray();
    $connection=$connection->whereRaw($column.' in ('.implode(',',$values).')');
}else {
    $connection=$connection->whereIn($column,$values);
}
return $connection;

优化失败,报 sql 错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘?’ at line 1
但是报的错误 sql 语句,在 sql 那边执行是没问题的

if (count($values)>300) {
    $values=collect($values)->map(function($value){
        return "'".$value."'";
    })->toArray();
    $values='('.implode(',',$values).')';
    $connection=$connection->whereRaw($column.' in ?',[$values]);
}else {
    $connection=$connection->whereIn($column,$values);
}
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 8

没看懂这个 if 有什么含义,直接 whereIn 不行吗

2年前 评论
ytoz (楼主) 2年前
luyang (作者) 2年前
ytoz (楼主) 2年前
ytoz (楼主) 2年前
luyang (作者) 2年前

不是个数, 是长度限制,max-allowed-packet 默认 4M

2年前 评论

whereIn 可以改成 join 查询

2年前 评论