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

如何从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);  
    
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 python 3des pyDes库
  • ¥15 关于#mysql#安装失败的问题。MySQL
  • ¥15 想问一下for循环计算表达式的方法,第一次接触
  • ¥15 如何在VA框架上面加功能,去读取框架内任何app数据功能
  • ¥15 关于#c语言#的问题:用c或c++写一个计算下列问题有关软件工程的代码并加上分析
  • ¥15 Zeppelin0.10.0版本升级lib包下的shiro-web
  • ¥15 链表入队的指针内存问题
  • ¥20 vba如何写本地html文件执行js
  • ¥15 VS2022的C#如何创建
  • ¥20 关于#用户注册#的问题,如何解决?