Codeigniter $this->db->get(),如何返回特定行的值? [英] Codeigniter $this->db->get(), how do I return values for a specific row?
本文介绍了Codeigniter $this->db->get(),如何返回特定行的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含三列的数据库表:ID、Name 和 Age.我需要找到具有特定(唯一)ID 的用户,然后返回年龄.目前,我正在使用以下代码
Say I have a database table with three columns: ID, Name, and Age. I need to find the user with a specific (unique) ID, and then return the age. Currently, I am using the following code
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
如何获取该用户的年龄?我想我必须使用
How do I go about getting the age for this user? I think I have to use
$q->result()
但不确定如何在一行中使用它.
But not sure how to use it with one row.
推荐答案
解决方案一
$this->db->where('id', '3');
// here we select every column of the table
$q = $this->db->get('my_users_table');
$data = $q->result_array();
echo($data[0]['age']);
解决方案二
// here we select just the age column
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();
echo($data[0]['age']);
解决方案三
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
// if id is unique, we want to return just one row
$data = array_shift($q->result_array());
echo($data['age']);
解决方案四(无活动记录)
$q = $this->db->query('SELECT age FROM my_users_table WHERE id = ?',array(3));
$data = array_shift($q->result_array());
echo($data['age']);
这篇关于Codeigniter $this->db->get(),如何返回特定行的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文