如何从Codeigniter中的数据库中获取数据..?

时间:2018-03-06 11:43:08

标签: php codeigniter

我在Codeigniter中从数据库中获取数据时遇到问题 它给出了一个错误 这是我的观点: -

       <tr>  
        <td><?php echo $row->Country_id;?></td>  
        <td><?php echo $row->country_name;?></td>  
        </tr>  

型号: -

     //data is retrive from this query  
     $query = $this->db->get('country');  
     return $query->result_array();  

控制器: -

     //load the database  
     $this->load->database();  
     //load the model  
     $this->load->model('select');  
     //load the method of model  
     $data['h']=$this->select->select();  
     //return the data in view  
     $this->load->view('select_view', $data);  

4 个答案:

答案 0 :(得分:2)

您的控制器

 $this->load->database();  
 //load the model  
 $this->load->model('select');  
 //load the method of model  
 $data['h']=$this->select->get_data();  //You can change the function name
 //return the data in view  
 $this->load->view('select_view', $data);

你的模特

$query = $this->db->get('country');
return $query->result_array();

您的观点

使用foreach循环显示您的数据

<tr> 
    <td><?php echo $row['Country_id'];?></td>  
    <td><?php echo $row['country_name'];?></td>  
 </tr> 

答案 1 :(得分:1)

我通过这个微小的改变来解决我的问题。 视图: -

 <?php foreach($h as $row):?>
    <tr>  
       <td><?php echo $row->Country_id;?></td>  
       <td><?php echo $row->country_name;?></td>  
    </tr>  
<?php endforeach; ?>

型号: -

  //data is retrive from this query  
 $query = $this->db->get('country');  
 return $query->result();  

控制器: -

 $this->load->database();  
 //load the model  
 $this->load->model('select');  
 //load the method of model  
 $data['h']=$this->select->select();  
 //return the data in view  
 $this->load->view('select_view', $data);  

答案 2 :(得分:0)

你可以试试这段代码:

<强>控制器

public function getdata(){
$condition_array = array();
                $data = '*';
                $result = $this->common->select_data_by_condition('Tablename', $condition_array, $data, $sortby = '', $orderby = 'ASC', $limit = '', $offset = '', $join_str = array());
}
print_r($result);

<强>模型

function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {

        $this->db->select($data);
        $this->db->from($tablename);

        //if join_str array is not empty then implement the join query
        if (!empty($join_str)) {
            foreach ($join_str as $join) {
                if (!isset($join['join_type'])) {
                    $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
                } else {
                    $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
                }
            }
        }

        //condition array pass to where condition
        $this->db->where($condition_array);


        //Setting Limit for Paging
        if ($limit != '' && $offset == 0) {
            $this->db->limit($limit);
        } else if ($limit != '' && $offset != 0) {
            $this->db->limit($limit, $offset);
        }

        //order by query
        if ($sortby != '' && $orderby != '') {
            $this->db->order_by($sortby, $orderby);
        }

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

        //if limit is empty then returns total count
        if ($limit == '') {
            $query->num_rows();
        }
        //if limit is not empty then return result array
        log_message('debug', 'fetching data result:' . $this->db->last_query());
        return $query->result_array();
    }

答案 3 :(得分:0)

如果您想访问$row->country_name之类的商品,则需要使用result()而不是result_array()