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.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?