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;
}
}
}
?>
You may try this simple way to join table. you may try this and can change
fields name
table name
andmethod
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;
}