0

I wanted to check whether the username exists in the database, but Codeigniter is throwing an

Error: Can't use method return value in write context.

The code is as follows:

public function check_username_exists($username){
    $query = $this->db->get_where('users', array('username' => $username));
    if(empty($query->row_array())){
        return true;
    }else{
        return false;
    }
}

4 Answers 4

3
$result = $query->row_array();
if(empty($result)){
   return true;
}else{
   return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

empty Only variables
0

try using below code

public function check_username_exists($username){
 $query = $this->db->get_where('users', array('username' => $username));
            if($query->num_rows() > 0){
                return true;
            }else{
                return false;
            }
        }

Comments

0

It looks like you are trying to validate a form for user registration. If this is the case, then you would be far better off using the built-in form_validation library (https://codeigniter.com/user_guide/libraries/form_validation.html). There is already a built in method that would help you make sure you have unique records (such as the username). So basically, once the form_validation library is loaded, you would need to set the rules for validation. Once rules are set, you can call the run() method, which will produce a bool true/false whether it passes validation or not.

$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', "trim|required|is_unique[users.username]");
if($this->form_validation->run() === true) {
    //Do something when posted form is validated
} else {
    // There were errors or this is the first time
    if(validation_errors())
        $data['error_message'] = validation_errors();
}

You should use the form validation library to validate any (and usually all) of your forms throughout your application

Hope that helps

1 Comment

Thank you. that was very helpful
0

First Understand the Error: Can't use method return value in write context.

empty() needs to access the value by reference (in order to check whether that reference points to something that exists).

However, the real problem you have is that you use empty() at all, mistakenly believing that "empty" value is any different from "false".

Empty is just an alias for !isset($thing) || !$thing. When the thing you're checking always exists (in PHP results of function calls always exist), the empty() function is nothing but a negation operator.

Not Coming to your problem, As Scott Miller suggest you can use CodeIgniter validation. And if you want a little advance solution then you can put your validation in the config folder with creating form_validation.php for more detail visit CodeIgniter documentation for validation.

$config = array(
    array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => 'required|is_unique[users.username]'
    )
);

And If you want to do it with the query then here is the query:

public function check_username_exists($username){
    $query = $this->db->get_where('users', array('username' => $username));
    if ($query->num_rows() > 0){
        return true;
    }
    else{
        return false;
    }
}

You can use num_rows to get the username exist or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.