繁体   English   中英

当我想使用代码点火器从我的数据库中访问某个列时,如何解决未定义的索引错误?

[英]How to solve undefined index error when I want to access a certain column from my database using code igniter?

我想验证登录用户的密码是否与我数据库中的密码匹配。 但是,由于某种原因,即使尝试回显它,我也无法提取该单个数据。 (echo $response['password'];) 但是 json_encode 正在工作,如 postman 结果所示。

Controller

function authentication(){
    $stdnum = $this->input->post('student_number');
    $password = $this->input->post('password');
    $response = $this->api_model->login($stdnum);
    // echo $password;
    // echo $response['password'];
    echo json_encode($response);
    $result = array();
    $result['login'] = array();
            
    if (isset($stdnum, $password)){

        if (password_verify($password, $response['password'])){ //line 95
            
            $index['id'] = $response['id'];
            $index['username'] = $response['username'];
            $index['student_number'] = $response['student_number'];
            $index['program'] = $response['program'];
            $index['email'] = $response['email'];

            array_push($result['login'], $index);

            $result['success'] = "1";
            $result['message'] = "success";
            echo json_encode($result);
            
            
            }
        else{
                $result['success'] = "0";
                $result['message'] = "error";
                echo json_encode($result);
            }
    }
    else{
        echo "null";
    }

}

Model

function login($stdnum){
    $this->db->where('student_number', $stdnum);
    $query = $this->db->get('mydatabase');
    return $query->row_array();
}

Postman 结果

[{"id":"4","username":"qwe","student_number":"213","program":"asd","email":"asd","password":"asd"}]
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message: Undefined index: password</p>
<p>Filename: controllers/api.php</p>
<p>Line Number: 95</p>


<p>Backtrace:</p>

仅将 model result_array() function 更改为 row_array(),因为 result_array 的结果在数组中,而 row_array() 结果在 object 中。

-> 使用这个 model function

function login($stdnum){
    $this->db->where('student_number', $stdnum);
    $query = $this->db->get('mydatabase');
    return $query->row_array();
}

暂无
暂无

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

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