在数据库表中查找ID并从列中选择值

时间:2012-11-07 06:58:04

标签: php codeigniter codeigniter-2

好的,首先请允许我概述一下我要做的事情。我想在我的视图中显示一个DIV,它只显示给某个“订阅”类型的用户。

所以我使用用户的会话ID在'subscriptions'表中搜索他们在数据库上的订阅(订阅以用户ID存储为主要订阅)。一旦找到他们的订阅行,我想从“订阅”列中获取值。如果值等于“freebie”,我想回显一个声明(我会在这里添加一个函数,一旦我明白它就可以了。)

这就是我想出来的......我做错了什么?不工作,我没有收到任何错误。

    // show appropiate upgrade message if user has free account

    $id = $this->session->userdata('user_id'); 

    $this->db->select('subscription');
    $this -> db -> where('id', '$id');
    $query = $this -> db -> get("subscriptions");
    $subscribe = $query -> result();

    if($subscribe == 'freebie')
    {
        echo 'function goes here';
    }

1 个答案:

答案 0 :(得分:1)

请尝试这个(我不确定你的列名,这是一个例子)

$id = $this->session->userdata('user_id'); 

$this->db->select('subscription');
$this->db->where('id', $id);
$query = $this->db->get("subscriptions");
$subscribe = $query->result_array();

if($subscribe[0]['subscription'] == 'freebie')
{
    echo 'function goes here';
}else{
   echo "No Result";
 }
相关问题