gpt4 book ai didi

php - 在 Fuelphp 查询生成器中使用 "()"

转载 作者:可可西里 更新时间:2023-11-01 00:32:02 25 4
gpt4 key购买 nike

我想像这样在 fuelphp 的 sql 中使用“()”。

select * from shop where (item1=$item1 or item2=$item1) and flag=on;

我试着这样表达;

$shop_query = DB::select()->from('shop');
$shop_query->where(function($shop_query){
$shop_query->where('item1','=',$item1)
->or_where('item2','=',$item1);
$shop_query ->and_where('flag','=','on');

但是,这会显示错误:undefined index item1.$item1,而且肯定有值。

我该如何解决这个问题?

最佳答案

您可以使用查询构建器的分组->where_open/close 方法:

public static function whatever($item1, ... the rest of your args)
{
$shop_query = DB::select()
->from('shop')
->where('flag', 'on')
->and_where_open()
->where('item1', $item1)
->or_where('item2', $item1)
->and_where_close()
->execute()->as_array(); // just change this to whatever you need

return $shop_query;
}

这变成:

SELECT * FROM `shop` WHERE `flag` = 'on' AND (`item1` = '$item1' OR `item2` = '$item1')

关于php - 在 Fuelphp 查询生成器中使用 "()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com