简体   繁体   English

codeigniter-从数据库中选择数据

[英]codeigniter - Selecting data from database

Sorry for the basic question, but I couldn't fidn the answer anywhere online. 对不起这个基本问题,但是我无法在网上任何地方找到答案。 I started with codeigniter yesterday And all I am trying to do is selecting data from my database. 我昨天开始使用codeigniter,而我要做的就是从数据库中选择数据。 However I don't get any errors nor does it show any data. 但是我没有任何错误,也没有显示任何数据。 If i change the database tabel name. 如果我更改数据库表名称。 It immediately throws an error. 它立即引发错误。

Model 模型

<?php

    class Customer_model extends CI_Model {

        public function get_customers($id) {
            if ($id != FALSE) {
                $query = $this->db->get_where('customer', array('id' => $id));
            } else {
                return FALSE;
            }
        }

    }

Controller 调节器

<?php

    class Customer extends CI_Controller {

        public function show($id) {
            $this->load->model('customer_model');
            $news = $this->customer_model->get_customers($id);

            $data['customer_name'] = $news['customer_name'];
            $data['streetname'] = $news['streetname'];
            $this->load->view('customers', $data);
        }

    }

VIEW 视图

<?php echo "hallo"; ?>
<?php echo $customer_name; ?>

** EDIT, in my page is can see the echo of hello. **编辑,在我的页面是可以看到你好的回声。 So I am in the correct page, but there are no errors nor notices of any data missing. 因此,我进入了正确的页面,但是没有错误,也没有任何数据丢失的通知。

If anyone have any idea what I'm doing wrong, thanks a zillion. 如果有人知道我在做什么错,谢谢。 btw database connectivity is included and also the package is loaded. btw数据库连接已包括在内,并且该软件包也已加载。

thanks 谢谢

In Model 在模型中

class Customer_model extends CI_Model {

    public function get_customers($id) { # refactored 

        $this->db->select(*);            
        $query = $this->db->get_where('customer', array('id' => $id));
        $result = $query->result_array();

        $count = count($result); 

        if(empty($count)){
            return false;
        }
        else{
            return $result;
        }
    }
}

In Controller 在控制器中

class Customer extends CI_Controller {

    public function show($id) {

        if(empty($id)) # Added
        { 
            echo "Token Invalid";
        }
        else{
            $this->load->model('customer_model');
            $news = $this->customer_model->get_customers($id);

            if($news == false){ # Added
                echo "No result Found";
            }
            else{
                $data['customer_name'] = $news[0]['customer_name']; # Changed
                $data['streetname'] = $news[0]['streetname']; # Changed
                $this->load->view('customers', $data);              
            }   
        }
    }
}

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

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