Codeigniter $ this-> db-> get(),如何返回特定行的值? [英] Codeigniter $this->db->get(), how do I return values for a specific row?

查看:402
本文介绍了Codeigniter $ this-> db-> get(),如何返回特定行的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据库表,包含三列:ID,名称和年龄。
我需要找到具有特定(唯一)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 clolumn of the table
$q = $this->db->get('my_users_table');
$data = $q->result_array();

echo($data[0]['age']);

SOLUTION TWO

//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 just one row to be returned
$data = array_shift($q->result_array());

echo($data['age']);

解决方案四(无活动记录) b

$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屋!

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