从Codeigniter查询返回一个单元格 [英] Returning one cell from Codeigniter Query
本文介绍了从Codeigniter查询返回一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查询一个表,只需要返回一个单元格。现在我可以想到的唯一方法是:
I want to query a table and only need one cell returned. Right now the only way I can think to do it is:
$query = $this->db->query('SELECT id FROM crops WHERE name = "wheat"');
if ($query->num_rows() > 0) {
$row = $query->row();
$crop_id = $row->id;
}
我想要的是,因为我选择'id'这是结果。 IE:$ query ='cropId'。
What I want is, since I'm select 'id' anyway, for that to be the result. IE: $query = 'cropId'.
任何想法?这是可能吗?
Any ideas? Is this even possible?
推荐答案
当然可以。只需在查询中使用 AND
:
Of course it's possible. Just use AND
in your query:
$query = $this->db->query('SELECT id FROM crops WHERE name = "wheat" AND id = {$cropId}');
或者您可以使用提供的 Active Record 类:
Or you could use the raw power of the provided Active Record class:
$this->db->select('id');
$this->db->from('crops');
$this->db->where('name','wheat');
$this->db->where('id',$cropId);
$query = $this->db->get();
如果你只是想要 cropId
整列:
foreach ($query->result()->id as $cropId)
{
echo $cropId;
}
尝试一下,我不知道是否可以工作: p>
Try this out, I'm not sure if it will work:
$cropId = $query->first_row()->id;
这篇关于从Codeigniter查询返回一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文