未知列不断出现

时间:2017-04-03 10:18:20

标签: mysql codeigniter

我已经阅读过CI的文档但是我仍然遇到有关查询的错误。我发布了答案,希望你能帮我解决这个问题。

    $code = $_SESSION['affiliate_code'];
    $email  = $this->db->query('select email from affiliates where referral_code = '.$code);

正在发生的事情是创建的查询看起来像这样

    select email from affiliates where referral_code = dTE7TkDcOa86;

但为了工作,它应该像

    select email from affiliates where referral_code = 'dTE7TkDcOa86';

如何让我的代码正常工作? 感谢您对此的帮助。谢谢。

4 个答案:

答案 0 :(得分:0)

试试这个:

$email  = $this->db->query("select email from affiliates where referral_code ='{$code}'");

但更好的是准备陈述:

$sql = $this->db->query("select email from affiliates where referral_code = ?"); 
$email = $this->db->execute($sql, array($code)); // Or something like this

答案 1 :(得分:0)

这样做:

$email  = $this->db->query("select email from affiliates where referral_code = '$code'");

答案 2 :(得分:0)

请尝试此

$email  = $this->db->query("select email from affiliates where referral_code = '".$code."' ");

使用Fallowing: -

$this->db->select('email')->from('affiliates')->where('referral_code',$code);
$query = $this->db->get();

答案 3 :(得分:0)

$email = $this->db->query("SELECT email FROM affiliates WHERE referral_code = '$code'");
    $email2 = $email->result_array();
    foreach($email2 as $key) {
        echo $key['email'];
    }