gpt4 book ai didi

php - Codeigniter 根据 id 返回用户名

转载 作者:行者123 更新时间:2023-11-29 00:54:26 25 4
gpt4 key购买 nike

我创建了一个帮助程序,用于在用户的唯一 ID 已知的情况下返回用户的用户名。

if ( ! function_exists('get_username'))
{
function get_username($user_id)
{
$ci=& get_instance();
$ci->load->database();

if (empty($user_id))
{
return FALSE;
}

$ci->db->select('username');
$ci->db->where('id', $user_id);
$ci->db->where('activated', 1);
$ci->db->where('banned', 0);
$ci->db->limit(1);
$query = $ci->db->get('users');

if ($query->num_rows() > 0) //if user exists
{
$row = $query->row();
return $row->username;
}
else
{
return FALSE;
}
}
}

例如,如果我尝试这样做,这在我看来是可行的:

echo get_username($this->uri->segment(3)); //uri segment 3 is a user id. 

但是想通过 Controller 将用户名发送到我的 View 。我在我的 Controller 中尝试了以下操作:

function write_message($user_id = '') //function parameter is 3rd uri segment
{
$data['username'] = get_username($user_id);
$this->load->view('my_view', $data);
}

那么在我看来我有

echo $username 回显 array 而不是用户名。我在这里做错了什么?

最佳答案

你的标准应该很清楚,我认为用户名应该是唯一的,所以......

if ($query->num_rows() == 1) //if user exists, and unique
{
$res = $query->result_array();
return $res[0]['username'];
}
else
{
return FALSE;
}

关于php - Codeigniter 根据 id 返回用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6803144/

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