简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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