简体   繁体   中英

How can I write this SQL query in the correct 'Codeigniter way'?

I'm currently expirementing with Codeigniter and I have a query within a model as follows;

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();
    }

This works and I'm happy with the result, however for the sake of good coding practice I'd like to write this using the documented CI format.

Can anyone please help me with this? I have read the documentation however have found myself a little puzzled at putting it into practice.

try like this:

 $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 =', '?');

try to follow this for more information: http://www.codeigniter.com/user_guide/database/active_record.html

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM