简体   繁体   English

从Codeigniter上的多个表中检索数据

[英]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. 我正在尝试从CI的多个表中检索数据,我一直在寻找类似的答案或示例,但没有找到。 Here is what I've done so far. 到目前为止,这是我所做的。

This is the function to retrieve the data I need on model 'anuncios_model.php' 这是在模型'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' 由于我只是测试自己的运气,所以这是我一直在控制器“ 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' 而我的看法'test.php'

<?php
    print_r($result);
?>

All I'm getting whenever I run 'anuncio/test' is: 每当我运行“ anuncio / test”时,我得到的就是:

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(). 而不是返回$ result = $ this-> db-> get(),我需要返回$ result-> result_array()。 $this->db->get returns some kind of strange object. $ this-> db-> get返回某种奇怪的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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