gpt4 book ai didi

php - Codeigniter $this->db->get(),我如何返回特定行的值?

转载 作者:IT王子 更新时间:2023-10-28 23:55:05 24 4
gpt4 key购买 nike

假设我有一个包含三列的数据库表:ID、姓名和年龄。我需要找到具有特定(唯一)ID 的用户,然后返回年龄。目前,我正在使用以下代码

$this->db->where('id', '3');
$q = $this->db->get('my_users_table');

如何获取该用户的年龄?我想我必须使用

$q->result()

但不确定如何在一行中使用它。

最佳答案

解决方案一

$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']);

关于php - Codeigniter $this->db->get(),我如何返回特定行的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com