codeigniter where =不能用于单词

时间:2014-07-07 16:34:34

标签: php codeigniter

以下代码无法正常运行:

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

如果$loggedID是像" justin"这样的单词,则会出错,但如果它的唯一数字如201110523,则可行。我不知道出了什么问题。用户中id的数据类型是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;                  
        }                
    }

1 个答案:

答案 0 :(得分:2)

当它是像“justin”这样的单词/字符串时,你必须转义变量:

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

或使用活动模式语法:

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