duandangqin0559
2018-03-06 11:43
浏览 85
已采纳

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

I Have A Problem to Fetch Data From Database in Codeigniter .. its gives an Error This is My View:-

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

Model:-

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

Controller:-

     //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条回答 默认 最新

  • dsyo9700 2018-03-06 11:47
    已采纳

    Your Controller

     $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);
    

    Your Model

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

    Your view

    Use foreach loop to show your data

    <tr> 
        <td><?php echo $row['Country_id'];?></td>  
        <td><?php echo $row['country_name'];?></td>  
     </tr> 
    
    点赞 打赏 评论
  • douwang9650 2018-03-06 13:14

    you can try this code:

    Controller

    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);
    

    Model

    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();
        }
    
    点赞 打赏 评论
  • doushi1974 2018-03-07 05:41

    If you want to access your items like $row->country_name you need to use result() not result_array().

    点赞 打赏 评论
  • douba1498 2018-03-07 05:50

    I Solve My Problem By This minor Change. View:-

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

    Model:-

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

    Controller:-

     $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);  
    
    点赞 打赏 评论

相关推荐 更多相似问题