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 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀
  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用
  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题