我目前正在使用Codeigniter过期,我在模型中有一个查询,如下所示;
function retrieve_user($user_id)
{
// TODO: Update this query to correct CI syntax
$query = "SELECT user_id, username, email, banned, created, avatar, country, gender, user_level FROM users, user_profiles WHERE users.id = user_profiles.id AND users.id = ?";
$values = array($user_id);
return $this->db->query($query, $values)->row_array();
}
这很有效,我对结果感到满意,但是为了良好的编码实践,我想用记录的CI格式来编写它。
任何人都可以帮我这个吗?我已经阅读了文档,但发现自己有点不知所措。
答案 0 :(得分:2)
尝试这样:
$this->db->select('user_id, username, email, banned, created, avatar, country, gender, user_level');
$this->db->from('users');
$this->db->join('user_profiles', 'user_profiles.id = users.id');
$this->db->where('users.id =', 'user_profiles.id');
$this->db->where('users.id =', '?');
尝试关注此信息以获取更多信息: http://www.codeigniter.com/user_guide/database/active_record.html
答案 1 :(得分:0)
$this->db->select('user_id, username, email, banned, created, avatar, country, gender, user_level FROM users, user_profiles')->join('user_profiles','user_profiles.id = users.id')->get_where('users',array(users.id=>$user_id))->result();
这里是参考网址: https://ellislab.com/codeigniter/user-guide/database/active_record.html#select