Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接3个表codeigniter php_Php_Codeigniter - Fatal编程技术网

连接3个表codeigniter php

连接3个表codeigniter php,php,codeigniter,Php,Codeigniter,我有三张桌子,客人信息,pg公司,用户档案 在来宾\用户\信息中有两列: g_uid | company_id company_id | user_id id |user_email 在pg_中,公司有两列: g_uid | company_id company_id | user_id id |user_email 在用户配置文件中,有两列: g_uid | company_id company_id | user_id id |user_email 这里我想从user\u

我有三张桌子,客人信息,pg公司,用户档案

在来宾\用户\信息中有两列:

g_uid | company_id
company_id | user_id
id |user_email
在pg_中,公司有两列:

g_uid | company_id
company_id | user_id
id |user_email
在用户配置文件中,有两列:

g_uid | company_id
company_id | user_id
id |user_email

这里我想从
user\u profile中获取
user\u email
。我
have
g\u uid
值(在
guest\u user\u info
表中)。我想从guest\u user\u info中获取公司id并与
pg\u company
表匹配,在那里我可以得到用户id。然后在
user\u profile
表中用id匹配
user\u id
。最后,我需要
user\u email
user\u profile
表中找到
user\u id

这是一个简单的问题,您只需要使用CodeIgniter中的主动查询就可以加入

$this->db->select("UP.id", "UP.user_email");
$this->db->from("guest_user_info AS GU");
$this->db->join("pg_company AS PC", "PC.company_id=GU.company_id");
$this->db->join("user_profile AS UP", "UP.id=PC.user_id");
$this->db->where("GU.g_uid", $guid);
$query = $this->db->get();

return $query->result();
在上面的代码中,
$guid
您必须提供您所拥有的

另外,请查看以下链接:

读完这篇文章,你会得到很多东西

   Check bellow code it`s working fine and  common model function also 
    supported more then one join and also supported  multiple where condition
 order by ,limit.it`s EASY TO USE and REMOVE CODE REDUNDANCY.

   ================================================================
    *Album.php
   //put bellow code in your controller
   =================================================================

    $album_id='';//album id 

   //pass join table value in bellow format
    $join_str[0]['table'] = 'pg_company';
    $join_str[0]['join_table_id'] = 'pg_company.company_id';
    $join_str[0]['from_table_id'] = 'guest_user_info.company_id';
    $join_str[0]['join_type'] = '';//set join type

    $join_str[1]['table'] = 'user_profile';
    $join_str[1]['join_table_id'] = 'user_profile.id';
    $join_str[1]['from_table_id'] = 'guest_user_info.user_id';
    $join_str[1]['join_type'] = '';


    $selected ="guest_user_info.*,user_profile.user_name,pg_company.name";
    $condition_array=array('guest_user_info.g_uid' => $g_uid);     
   $albumData= $this->common->select_data_by_condition('guest_user_info', $condition _array, $selected, '', '', '', '', $join_str);
                               //call common model function

    if (!empty($albumData)) {
        print_r($albumData); // print album data
    } 

 =========================================================================
   Common.php
    //put bellow code in your common model file
 ========================================================================

   function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {
    $this->db->select($data);
    //if join_str array is not empty then implement the join query
    if (!empty($join_str)) {
        foreach ($join_str as $join) {
            if ($join['join_type'] == '') {
                $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
            } else {
                $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
            }
        }
    }

    //condition array pass to where condition
    $this->db->where($condition_array);


    //Setting Limit for Paging
    if ($limit != '' && $offset == 0) {
        $this->db->limit($limit);
    } else if ($limit != '' && $offset != 0) {
        $this->db->limit($limit, $offset);
    }
    //order by query
    if ($sortby != '' && $orderby != '') {
        $this->db->order_by($sortby, $orderby);
    }

    $query = $this->db->get($tablename);
    //if limit is empty then returns total count
    if ($limit == '') {
        $query->num_rows();
    }
    //if limit is not empty then return result array
    return $query->result_array();
}