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; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dreamevil0002 2018-05-22 04:54
    关注

    Once update this code and check

    public function _consruct(){
        parent::_construct();
        $this->load->database();
    }
    
    function getData(){
        $data = $this->db->query("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");
         return $data;
    }
    
    评论
  • dpik71879 2018-05-22 05:57
    关注

    Actually, at the end of 2 hrs of the journey, I found that I forgot to configure new database.

    All the answers were correct in this case.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 算法问题 斐波那契数 解答
  • ¥20 JAVAscript
  • ¥15 VS2019 SPY++ 获取句柄操作
  • ¥15 Facebook 获取广告
  • ¥15 java文本解密算法
  • ¥15 有没有办法等sql查询完成后执行下一步操作
  • ¥15 android8以下机子自动连接指定wifi热点
  • ¥15 python下sqlserver数据表变化监听
  • ¥50 VC中怎么用sql语言联合查询多个加密数据库?
  • ¥65 要通过ADB 命令,修改 开发者选项里面的瀑布刘海屏选项,来个会的chatGPT早就问过了, 不要般