dongshuiga2826 2015-08-08 10:49
浏览 143
已采纳

PHP错误 - 不能将标量值用作数组

Hello I am a beginner in codeigniter framework and I have some problems.

On my index page i have login form, and when user insert username and password and clicks submit button he calls controller's method login whose task is to collect username and password from form and to pass the data to models method "login($username, $password)" and then models method returns true or false depending on whether we have valid user in database or not, and then controller's method either pass the user further or returns him on the beginning. My problem is that i want to know users access level, and i made this method in model

public function users_level($username){

    $query = $this->db->get_where('users',array('username' => $username) );

    $users_level = $query->result();    // I tried with these too $users_level[] = $query->result();    

    return $users_level;
}

with which i want to return user's access level, and to use that information in order to determine what view to present for that specific user.

This is login method from controller:

public function login(){
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('model_user');
        $result = $this->model_user->login($username,$password);
        if ($result == true){     
                $result['level'] = $this->model_user->users_level($username);    //i believe that mistake is maybe in this line
                $this->welcome();   //I'm going to send information to welcome method about users level of access                  
            }else if ($result == false){
                $this->index();
            }
}

And this is an error that occurs

Severity: Warning

Message: Cannot use a scalar value as an array

Filename: controllers/controler_user.php

Line Number: 33

Backtrace:

File: C:\wamp\www\ci_project_2015\application\controllers\controler_user.php
Line: 33
Function: _error_handler

File: C:\wamp\www\ci_project_2015\index.php
Line: 292
Function: require_once
  • 写回答

1条回答 默认 最新

  • dongwei1263 2015-08-08 11:43
    关注

    You are trying to use $result as an array when it is already initialized as a boolean. You could reinitialize it as an array using:

    $result = array('level' => $this->model_user->users_level($username));
    

    or

    $result = array();
    $result['level'] = $this->model_user->users_level($username);
    

    However, this is a bad idea since you are using the same variable for different things. A better solution is to rename one of the variables e.g.

    $logged_in = $this->model_user->login($username,$password);
    if ($logged_in == true){
    

    Or better yet, since the boolean is only being used once, you can skip the initialization of $logged_in and use the result of $this->model_user->login($username,$password) directly in the condition

    if ($this->model_user->login($username,$password)) {
    

    You can omit the == true since it returns a boolean.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?