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条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集