codeigniter where =不能用于单词[关闭]

The following code is not working properly:

$query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" ); 

It errors if the $loggedID is words like "justin", but if its only number like 201110523, it works. I don't know what is wrong. The datatype of the id in users is varchar.

public function account_type_student(){
    $loggedID = $this->input->post('id'); 

    $query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" );

    foreach ($query->result() as $row)
    {
        $query = $row->accountType;
    }

    if($query=="student"){    
        return true;
    }
    else{
        return false;                  
        }                
    }

When it is a word / string like "justin" then you have to escape your variable:

$query = $this->db->query("SELECT accountType FROM users WHERE id = '$loggedID' " ); 

Or use the active pattern syntax:

$this->db->select('accountType');
$this->db->where('id', $loggedID);
$query = $this->db->get('users');