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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵