php 将数组传递到 Codeigniter Active Record 中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13717555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:56:37  来源:igfitidea点击:

Pass array to where in Codeigniter Active Record

phpcodeigniteractiverecord

提问by Daniel White

I have a table in my database with adminId and clientId

我的数据库中有一个表,其中包含 adminId 和 clientId

There might be 20 records with the adminId of the logged in user and I'm trying to pull a list of clients.

登录用户的 adminId 可能有 20 条记录,我正在尝试提取客户端列表。

I am wondering if there is a way i can say something like:

我想知道是否有办法可以这样说:

$this->db->where('id', '20 || 15 || 22 || 46 || 86');

I'm trying to do this with dynamic data (you never know how many clients Id's you'll need to pull). Any ideas?

我正在尝试使用动态数据执行此操作(您永远不知道需要提取多少个客户端 ID)。有任何想法吗?

回答by joHN

$this->db->where_in('id', ['20','15','22','42','86']);

Reference: where_in

参考:where_in

回答by Stanley

Use where_in()

使用where_in()

$ids = array('20', '15', '22', '46', '86');
$this->db->where_in('id', $ids );

回答by Osiris

From the Active Record docs:

来自 Active Record 文档:

$this->db->where_in();

Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate

如果合适,生成一个 WHERE 字段 IN ('item', 'item') SQL 查询与 AND 连接

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')

回答by Thiyagu

Generates a WHERE field IN (‘item', ‘item') SQL query joined with AND if appropriate,

生成一个 WHERE 字段 IN ('item', 'item') SQL 查询,如果合适,用 AND 连接,

$this->db->where_in()
ex :  $this->db->where_in('id', array('1','2','3'));

Generates a WHERE field IN (‘item', ‘item') SQL query joined with OR if appropriate

生成一个 WHERE 字段 IN ('item', 'item') SQL 查询,如果合适的话用 OR 连接

$this->db->or_where_in()
ex :  $this->db->where_in('id', array('1','2','3'));