Php 如何获得CodeIgniter 4中的特定列?

Php 如何获得CodeIgniter 4中的特定列?,php,codeigniter-4,Php,Codeigniter 4,我想从表中选择特定的列。不是所有的列 $this->userModel->where($where)->first(); 我正在使用它,但它会返回所有数据。根据您可以使用选择() $db = \Config\Database::connect(); $builder = $db->table('mytablename'); // 'mytablename' is the name of your table $builder->selec

我想从表中选择特定的列。不是所有的列

$this->userModel->where($where)->first();
我正在使用它,但它会返回所有数据。

根据您可以使用选择()

$db      = \Config\Database::connect();
$builder = $db->table('mytablename');        // 'mytablename' is the name of your table

$builder->select('userid','username');       // names of your columns
$builder->where('userid', 5);                // where clause
$query = $builder->get();

return $query;
可能有4种使用方式,您可以选择


这些内容应该放在模型文件中,模型文件的方法将返回数据库数据集。

您知道这些列的名称吗?
$this->userModel->where($where)
的语法对我来说很奇怪。您想在模型中使用一些分离的方法来选择返回吗?do
print\r($this->userModel->where($where)->first()
告诉我们你得到了什么。是的,我知道列的名称。我如何选择像userid,username only你的问题不清楚,但现在,我得到的是你可以在哪里使用
。我想选择特定列,就像我们在Ci3 select上做的那样('userid,username'))这就解决了我的问题,非常感谢你获取记录我在code$query->getFirstRow($This->tempReturnType)@user1951608中添加了这个东西,不用谢!现在你有了CI4文档)很奇怪,CI3和CI4有不同的网站,我以为他们已经更新了CI3网站,但没有,他们已经创建了独立的网站。CI4比CI3改进了很多,而且CI3的用户基数也更高,所以在我看来,单独的网站更好。