简体   繁体   English

select 来自多列:codeIgniter

[英]select from multiple columns: codeIgniter

I want to select multiple columns in a table using codeIgniter active records.我想使用 codeIgniter 活动记录 select 表中的多个列。 Selecting using选择使用

    $this->db->select('*');
    $this->db->from($this->table_name);

but selecting a number of columns eg但选择了许多列,例如

   $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.根据 CodeIgniter 用户指南,他们给出了以下示例。 so I thought it should work in my case.所以我认为它应该适用于我的情况。

$this->db->select('title, content, date');

$query = $this->db->get('mytable');

// Produces: SELECT title, content, date FROM mytable

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.第二个查询不会像您期望的那样工作,因为select()需要一个字符串或数组作为第一个参数,而不是采用无限的 arguments。 In your example, only username would be selected.在您的示例中,只会选择username The correct string syntax is this:正确的字符串语法是这样的:

$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 documentation instead.我可以与您分享更多内容,但我建议您通过Active Record 文档再阅读一两遍。 Good luck, and enjoy Codeigniter!祝你好运,享受 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 one argument (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 .请注意(正确的)示例2中每个列名周围没有引号。 In example 1, there are several arguments passed to the function, and only the first one is used, while the rest are ignored.在示例 1 中,有几个 arguments 传递给 function,仅使用第一个,而忽略 rest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM