从多个列中选择:codeIgniter [英] select from multiple columns: codeIgniter

查看:136
本文介绍了从多个列中选择:codeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用codeIgniter活动记录在表中选择多个列。选择使用

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

  $ this-> db-> select('username','email','last_login','created' ,'modified','group_id'); 
$ this-> db-> from($ this-> table_name);

不工作。它只返回一个数组,其中包含第一列中的值。



根据codeIgniter的用户指南他们给了以下例子,所以我认为它应该在我的情况下工作。

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

$ query = $ this-> db-> get('mytable');
$ b b //产生:SELECT title,content,date from mytable


解决方案

p>第二个查询将无法正常工作,因为 select()需要一个字符串或数组作为第一个参数,而不是无限的参数。在您的示例中,将只选择 username 。正确的字符串语法是:

  $ this-> username,email,last_login,created,modified,group_id'); 

我可以与你分享更多,但我建议你通过< a href =http://codeigniter.com/user_guide/database/active_record.html =nofollow> Active Record文档。

:请注意这两个例子的不同之处:



1 - 此行以单独的参数传递每个列:

  $ this-> db 
- > select('username','email','last_login','created','modified','group_id');

2 - 此行正确传递一个参数(逗号分隔字符串):

  $ this-> db 
- > select ,last_login,created,modified,group_id');

注意在(正确)示例中每个列名称没有引号 2 <强>。在示例1中,有几个参数传递给函数,只有第一个参数被使用,而其余参数被忽略。


I want to select multiple columns in a table using codeIgniter active records. Selecting using

    $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 shouold I do this?

according to the codeIgniter user guide they gave the following example. 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. In your example, only username would be selected. 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. Good luck, and enjoy Codeigniter!

AFTER YOUR EDIT: Please note the differences in these two examples:

1 - This line passes each column as a separate argument:

$this->db
    ->select('username','email','last_login','created','modified','group_id');

2 - This line correctly passes one argument (comma separated string):

$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.

这篇关于从多个列中选择:codeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆