简体   繁体   English

从数据库获取查询-Codeigniter

[英]getting the query from database - Codeigniter

I wanted to check whether the username exists in the database, but Codeigniter is throwing an 我想检查用户名是否存在于数据库中,但是Codeigniter抛出了一个

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;
    }
}
$result = $query->row_array();
if(empty($result)){
   return true;
}else{
   return false;
}

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;
            }
        }

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 ). 如果是这种情况,那么使用内置的form_validation库( 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. 因此,基本上,一旦加载了form_validation库,就需要设置验证规则。 Once rules are set, you can call the run() method, which will produce a bool true/false whether it passes validation or not. 设置规则后,您可以调用run()方法,无论是否通过验证,该方法都会产生布尔值true / false。

$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 希望能有所帮助

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). empty()需要通过引用访问该值(以检查该引用是否指向存在的对象)。

However, the real problem you have is that you use empty() at all, mistakenly believing that "empty" value is any different from "false". 但是,真正的问题是您根本使用了empty(),错误地认为“ empty”值与“ false”有所不同。

Empty is just an alias for !isset($thing) || !$thing 空只是!isset($thing) || !$thing的别名!isset($thing) || !$thing !isset($thing) || !$thing . !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. 当您要检查的事物始终存在时(在PHP中,函数调用的结果始终存在), empty()函数不过是一个否定运算符。

Not Coming to your problem, As Scott Miller suggest you can use CodeIgniter validation. 没问题,正如Scott Miller建议的那样,您可以使用CodeIgniter验证。 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 . 并且,如果您需要一些高级解决方案,则可以通过创建form_validation.php将验证放入config文件夹中以获取更多详细信息,请访问CodeIgniter文档进行验证

$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. 您可以使用num_rows来获取用户名是否存在。

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

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