database CodeIgniter - 只返回一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4280235/
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-09-08 07:55:53  来源:igfitidea点击:

CodeIgniter - return only one row?

databasecodeigniter

提问by Hailwood

At the moment if I am doing a query on the database that should only return one row, using:

目前,如果我对应该只返回一行的数据库进行查询,请使用:

...query stuff...
$query = $this->db->get();
$ret = $query->result();
return $ret[0]->campaign_id;

Is there a CodeIgniter function to return the first row? something like $query->row();

是否有返回第一行的 CodeIgniter 函数?就像是$query->row();

Or even better would be the ability to, if there was only one row, to just use the query object directly.

或者甚至更好的是能够,如果只有一行,直接使用查询对象。

e.g. $query->campaign_id;

例如 $query->campaign_id;

回答by Alisson

You've just answered your own question :) You can do something like this:

你刚刚回答了你自己的问题 :) 你可以做这样的事情:

$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;

You can read more about it here: http://www.codeigniter.com/user_guide/database/results.html

您可以在此处阅读更多相关信息:http: //www.codeigniter.com/user_guide/database/results.html

回答by Suresh Kamrushi

This is better way as it gives you result in a single line:

这是更好的方法,因为它为您提供了一行结果:

$this->db->query("Your query")->row()->campaign_id;

回答by Malachi

To add on to what Alisson said you could check to see if a row is returned.

要添加 Alisson 所说的内容,您可以检查是否返回了一行。

// Query stuff ...
$query = $this->db->get();

if ($query->num_rows() > 0)
{
    $row = $query->row(); 
    return $row->campaign_id;
}

return null; // or whatever value you want to return for no rows found

回答by bnp887

To make the code clear that you are intending to get the first row, CodeIgniter now allows you to use:

为了让代码清楚表明您打算获取第一行,CodeIgniter 现在允许您使用:

if ($query->num_rows() > 0) {
    return $query->first_row();
}

To retrieve the first row.

检索第一行。

回答by Maciej

$this->db->get()->row()->campaign_id;

回答by Bloody Programmer

If you require to get only one record from database table using codeigniter query then you can do it using row(). we can easily return one row from database in codeigniter.

如果您只需要使用 codeigniter 查询从数据库表中获取一条记录,那么您可以使用 row() 来完成。我们可以轻松地从 codeigniter 中的数据库返回一行。

$data = $this->db->get("items")->row();

回答by lalitpatadiya

Change only in two line and you are getting actually what you want.

只改变两行,你就会得到你想要的。

$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;

try it.

尝试一下。

回答by Shakawat Hossain

We can get a single using limit in query

我们可以在查询中得到一个使用限制

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

 $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

回答by Shipra Bohra Aashhiiii

class receipt_model extends CI_Model {

类收据_模型扩展 CI_Model {

   public function index(){

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

      $this->db->from('donor_details');

      $this->db->order_by('donor_id','desc');

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

      $row=$query->row();

      return $row;
 }

}

}