Php 如何在Codeigniter中从数据库获取数据。。?

Php 如何在Codeigniter中从数据库获取数据。。?,php,codeigniter,Php,Codeigniter,我在Codeigniter中从数据库获取数据时遇到问题。。 它给出了一个错误 我的看法是:- <tr> <td><?php echo $row->Country_id;?></td> <td><?php echo $row->country_name;?></td> </tr> 控制器:- //loa

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

       <tr>  
        <td><?php echo $row->Country_id;?></td>  
        <td><?php echo $row->country_name;?></td>  
        </tr>  
控制器:-

     //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);  
 $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);  
你的控制器

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

您可以尝试以下代码:

控制器

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

如果您想访问您的项目,如
$row->country\u name
,您需要使用
result()
而不是
result\u 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);  
 $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);  

您传递的
$h
,其中
$row
来自数据库:-创建表country(country_id int,country_name varchar(255))$row来自foreach loopTime来点击文档,听起来好像您还没有走出大门:最重要的是:您遇到的确切错误是什么?如问题的注释所示,他已经使用了foreach循环
//如果limit为空,则返回total count if($limit=''){$query->num rows();}
这没有任何作用。还有,这个代码在什么方面解决了与这个问题相关的问题?嗯?这有什么关系?
 $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);