简体   繁体   中英

Retrieving data from multiple tables on Codeigniter

I'm trying to retrieve data from multiple tables on CI, I've been looking for a similar answer or example but didn't found one. Here is what I've done so far.

This is the function to retrieve the data I need on model 'anuncios_model.php'

public function return_all_for_id($id){
    $this->db->select('anuncios.*');
    $this->db->select('usuarios.id, usuarios.link, usuarios.nombre');
    $this->db->select('departamentos.nombre_departamento');
    $this->db->select('categorias.nombre, categorias.link');
    $this->db->select('subcategorias.nombre, subcategorias.link');
    $this->db->from('anuncios, usuarios, departamentos, categorias, subcategorias');
    $this->db->where('anuncios.id_anuncio', $id);
    $where = 'anuncios.id_cat = categorias.id AND anuncios.id_subcat = subcategorias.id AND anuncios.id_user = usuarios.id AND anuncios.id_departamento = departamentos.id_departamento';
    $this->db->where($where);
    return $result = $this->db->get();
}

Since I was just testing my luck, this is the function I've been calling on my controller 'anuncio.php'

public function test(){
    $data['result'] = $this->anuncios_model->return_all_for_id(1);
    $this->load->view('pages/test' ,$data);
}

And my view 'test.php'

<?php
    print_r($result);
?>

All I'm getting whenever I run 'anuncio/test' is:

CI_DB_mysql_result Object ( [conn_id] => Resource id #29 [result_id] => Resource id #38 [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => )

Am I missing something?

you can also try this:

$query = $this->db->get();

return $query->result();

Actually, my approach was almost good. Instead of returning $result = $this->db->get() I needed to return $result->result_array(). $this->db->get returns some kind of strange object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM