如何将数据从数据库从模型发送到控制器代码

时间:2015-07-05 14:55:04

标签: php codeigniter

模型

public function sign_in()
{
    if (isset($_POST)) {
        $this->load->library('session');
        $Email = $this->input->post('Email');
        $Password = $this->input->post('Password');
        $this->db->select('id', 'Name', 'Password', 'Email');
        $this->db->from('users');
        $this->db->where('Email', $Email);
        $this->db->where('Password', md5($Password));
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $data = array();
            foreach ($query->result() as $row)
            {
                $data[] = array(
                    'Name' => $row->Name
                );
            }
            return $data;
        } else {
            return false;
        }
    }
}

//控制器

public function index()
{
    $this->load->model('Login');
    $data = $this->Login->sign_in();

    if ($data) {
        $this->load->view('index', $data);
        echo 'success';
        print_r($data);
    } else {
        $this->load->view('index');
    }
}

//结果

enter image description here

3 个答案:

答案 0 :(得分:2)

这里的问题是你的模型,特别是你的查询。

  1. 您的SELECT设置为检索以下内容:'id', 'Name', 'Password', 'Email'但实际上(根据您的代码),您只需要Name

  2. 您正在创建一个不必要的数组。您可能知道或不知道,$query->result()是一个返回对象数组的Codeigniter函数。因此,您不需要迭代它并创建另一个数组。您需要做的就是返回这些结果,让控制器使用->运算符进行迭代,以获取对象数据。

  3. 尽管如此,这些是我在当前模型方法中要处理的错误。我用评论来解释:

    public function sign_in()
    {
        if (isset($_POST)) { //POST info should be set in the controller, not in the model
            $this->load->library('session'); //Why do you need this??
            $Email = $this->input->post('Email'); ///POST info should be set in the controller, not in the model
            $Password = $this->input->post('Password');//POST info should be set in the controller, not in the model
            $this->db->select('id', 'Name', 'Password', 'Email'); // why do you require all these, if you are only returning the NAME ?
            $this->db->from('users');
            $this->db->where('Email', $Email);
            $this->db->where('Password', md5($Password));
            $this->db->limit(1); // why limit, if there should already only be one account that matches?
            $query = $this->db->get();
    
            //the code below is iterating for no purpose.
            //If the reason why youre doing this iteration is to obtain arrays rather than arrays of objects,
            //then use $this->db->result_array() instead
            //also, the conditional is not necessary as it will already return false (0) if none found.
            if ($query->num_rows() > 0) {
                $data = array();
                foreach ($query->result() as $row) {
                    $data[] = array(
                        'Name' => $row->Name
                    );
                }
                return $data;
            } else {
                return false;
            }
        }
    }
    

    我会像这样重写你的代码:

    <强> MODEL:

    public function sign_in($Email, $Password) {
            $this->db->select('Name');
            $this->db->from('users');
            $this->db->where('Email', $Email);
            $this->db->where('Password', md5($Password));
            $query = $this->db->get();
            return $query->row();
        }
    }
    

    <强>控制器:

    public function index() {
        $data = array();
        if(isset($_POST)){
            $this->load->model('Login');
            $Email = $this->input->post('Email');
            $Password = $this->input->post('Password');
            $result = $this->Login->sign_in($Email, $Password);
            if ($result) {
                $data["user_info"] = $result;
            } 
        }
        $this->load->view('index', $data);
    }
    

    查看:

    print_r($user_info);
    //or
    echo $user_info->Name;
    

答案 1 :(得分:0)

尝试将模型中的if语句更改为:

if ($query->num_rows() > 0) {
    $data = array('name' = $query->result()->row->Name);
    return $data;
} else {
    ...
}

问题源于您如何将值分配给$data数组。我也简化了if语句中的逻辑;由于您只通过有效记录查询中的limit(1)返回一行,因此您不需要foreach

答案 2 :(得分:0)

尝试以下代码未经测试。另外在控制器上你使用了我认为可能会混淆的$ data,所以把它改成了user_info codeigniter而你没有在控制器上设置任何名称的变量

模型

public function sign_in()
{
if (isset($_POST)) {

    $this->load->library('session');

    $email = $this->input->post('email');
    $password = $this->input->post('password');

    // Check is the same on table in database case sensitive I think
    $this->db->select('id', 'name', 'email');
    $this->db->from('users');
    $this->db->where('email', $email);

    // I would not use MD5 Not Secure Any More
    $this->db->where('password', md5($password));

    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->row_array();
    } else {
        return false;
    }
}

控制器

public function index() {
$this->load->model('Login');

$user_info = $this->Login->sign_in();

if ($user_info) {
    $data['id'] = $user_info['id'];
    $data['name'] = $user_info['name'];
    $data['email'] = $user_info['email'];

    $this->load->view('index', $data);
    echo 'success';
    print_r($user_info);

} else {

    $this->load->view('index');

}

}   

观看

<?php echo $name;?>

我不确定您在提交表单时是否使用codeigniter的任何表单验证

http://www.codeigniter.com/userguide2/libraries/form_validation.html