首页 > 解决方案 > 从数据库 codeignter 获取用户数据

问题描述

我在一个数据库中有两个表,表一称为:ci_admin,表二称为 active_ingredient。

我想使用当前用户会话从 ci_admin 表中的 user_id 等于 admin_id 的 active_ingredient 表中获取用户数据。

这段代码,获取所有用户的数据,我只需要用户插入的数据:

        $wh =array();
        $SQL ='SELECT * FROM active_ingredient';
        $wh[] = "SELECT 1
        FROM   ci_admin
        WHERE  ci_admin.admin_id = active_ingredient.user_id";

        if(count($wh)>0)
        {               $WHERE = implode(' and ',$wh);
            return $this->datatable->LoadJson($SQL,$WHERE);
        }
        else
        {
            return $this->datatable->LoadJson($SQL);
        }

标签: sqlsession

解决方案


试试这个,它会帮助你。

    $this->db->select('ai.*,ca.*'); 
    $this->db->from('active_ingredient ai');
    $this->db->join('ci_admin ca', 'ca.admin_id = ai.user_id', 'left');
    $this->db->where($data); //pass the session data for exact filter
    $query = $this->db->get();
    return $query->result();

推荐阅读