php 从多列中选择:codeIgniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6112972/
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
select from multiple columns: codeIgniter
提问by serengeti12
I want to select multiple columns in a table using codeIgniter active records. Selecting using
我想使用 codeIgniter 活动记录选择表中的多列。选择使用
$this->db->select('*');
$this->db->from($this->table_name);
but selecting a number of columns e.g.
但选择一些列,例如
$this->db->select('username','email','last_login','created','modified','group_id');
$this->db->from($this->table_name);
does not work.It only returns an array with the values from the first column. How should I do this?
不起作用。它只返回一个包含第一列值的数组。我该怎么做?
according to the CodeIgniter user guide they gave the following example. so I thought it should work in my case.
根据 CodeIgniter 用户指南,他们给出了以下示例。所以我认为它应该适用于我的情况。
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
回答by Wesley Murch
The second query will not work as you expect, as select()
expects a string or array as the first parameter rather than taking unlimited arguments. In your example, only username
would be selected. The correct stringsyntax is this:
第二个查询不会像您期望的那样工作,因为select()
期望字符串或数组作为第一个参数,而不是采用无限参数。在您的示例中,仅username
会被选中。正确的字符串语法是这样的:
$this->db->select('username, email, last_login, created, modified, group_id');
There's much more I could share with you, but I suggest you have another read or two through the Active Record documentationinstead. Good luck, and enjoy Codeigniter!
我可以与您分享更多内容,但我建议您再阅读一两遍 Active Record 文档。祝你好运,享受 Codeigniter!
AFTER YOUR EDIT: Please note the differences in these two examples:
编辑后:请注意这两个示例中的差异:
1- This line passes each column as a separate argument:
1- 此行将每一列作为单独的参数传递:
$this->db
->select('username','email','last_login','created','modified','group_id');
2- This line correctly passes oneargument (comma separated string):
2- 这一行正确传递了一个参数(逗号分隔的字符串):
$this->db
->select('username, email, last_login, created, modified, group_id');
Note the absence of quotes around each column name in the (correct) example 2. In example 1, there are several arguments passed to the function, and only the first one is used, while the rest are ignored.
请注意(正确的)示例2 中每个列名周围没有引号。在示例 1 中,有多个参数传递给函数,仅使用第一个参数,而忽略其余参数。