duandang6352 2018-05-22 04:25
浏览 47
已采纳

如何在codeigniter中获取用户列表?

I am trying to get list of users from Users table in codeigniter. My problem is that if am using following code than try to print out the result, it's giving me blank array while printing query using last_query() function and running this query in phpmyadmin is giving correct result.

My code in model

$data = $this->db->select('u.*')
                    ->from('users u')
                    ->join('users_groups ug','ug.user_id = u.id')
                    ->join('groups g','g.id = ug.group_id')
                    ->where('g.id',7)
                    ->get()
                    ->result();

this code is giving me blank array then what i did is print the query using following code

echo $this->db->last_query();

and this is what query was

SELECT `u`.* FROM `users` `u` JOIN `users_groups` `ug` ON `ug`.`user_id` = `u`.`id` JOIN `groups` `g` ON `g`.`id` = `ug`.`group_id` WHERE `g`.`id` = 7

which is working fine. but i am trying to print the data using print_r($data) is giving me blank array.

Can anyone tell me what can be the issue ?

edited

My full model code.

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Technician_model extends CI_Model {

public function _consruct(){
    parent::_construct();
}

function getData(){
    $data = $this->db->select('u.*')
                    ->from('users u')
                    ->join('users_groups ug','ug.user_id = u.id')
                    ->join('groups g','g.id = ug.group_id')
                    ->where('g.id',7)
                    ->get()
                    ->result();
     return $data;
}

function getRecord($user_id){
    $record = $this->db->get_where('users',array('id'=>$user_id))->row();
    echo '<pre>';
    print_r($record);
    exit;
    return $record;
}

function addModel($data){

    if($this->db->insert('users',$data)){
        return  $this->db->insert_id();
    }
    else{
        return FALSE;
    }
}

function editModel($data,$user_id){

    $this->db->where('id',$user_id);

    if($this->db->update('users',$data)){
        return true;
    }
    else{
        return false;
    }
}

function deleteModel($user_id){

    $this->db->where('id', $user_id);

    if($this->db->delete('users')){
        return true;
    }
    else{
        return FALSE;
    }
 }
 }
 ?>
  • 写回答

3条回答 默认 最新

  • doufu8887 2018-05-22 06:22
    关注

    You may try this simple way to join table. you may try this and can change fields name table name and method according to your require.

    public function getUser() {
        $this->db->select('users');
        $this->db->from('Users');
        $this->db->where('users_groups.user_id',1);
        $this->db->join('users_groups', 'users.id = users_groups.user_id');
        $query = $this->db->get();
        $result = $query->result_array();
    
       var_dump($result); // display data in array();
       return $result; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)