ThinkPHP5.1x数据查询表达式报错,和5.0x版的有区别?

发布于 2022-09-12 13:47:33 字数 518 浏览 18 评论 0

原来5.0x版本的原来可以这么写,没有报错并且正常查询到数据!

$ids = array(
    0 => 121,
    1 => 125,
    2 => 135
);
$where=[];
$where['site_id'] = ['in',$ids];
$data = Db::name('site')->where($where)->count();

但是在5.1x版报 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

问:在5.1x版本中这个数组查询表达式应该怎么写?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2022-09-19 13:47:33

楼上正解 或者 使用

$data = Db::name('site')->whereIn($ids)->count();
$ids = array(
    0 => 121,
    1 => 125,
    2 => 135
);
$where=[];
array_push($where, ["id", "In", $ids]);
$data = Db::name('site')->where($where)->count();

个人更喜欢下面这种

感性 2022-09-19 13:47:33

明显就是

$where['site_id'] = ['in',$ids];

这句写错了,改成

$where['site_id'] = ['in',implode(',',$ids)];

遇到报错不要需要考虑其它的,先把sql语句打印出来再说

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文