php CodeIgniter - 在非对象上调用成员函数 select()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8322124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:26:20  来源:igfitidea点击:

CodeIgniter - Call to a member function select() on a non-object

phpcodeigniter

提问by softboxkid

I'm quite new to CodeIgniter. This is my code:

我对 CodeIgniter 很陌生。这是我的代码:

class User_model extends CI_Model {

    function validate_user() {

        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $validate_user = $this->db->get();

        if($validate_user->num_rows == 1) {
            return TRUE;
        }
    }
}

I'm, receiving this error in my model file:

我在我的模型文件中收到此错误:

Call to a member function select() on a non-object

Currently I'm using CodeIgniter version 2.1.0. Please help me!

目前我使用的是 CodeIgniter 2.1.0 版。请帮我!

回答by Vasil Dakov

I think that you have to load the "database" library. The first way is to include the "database" in your application/config/autoload.php

我认为您必须加载“数据库”库。第一种方法是在您的 application/config/autoload.php 中包含“数据库”

$autoload['libraries'] = array('database', 'session');

or in your class constructor:

或在您的类构造函数中:

class User_model extends CI_Model { 

     public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }
}

You can get more information here: https://www.codeigniter.com/user_guide/database/connecting.html

您可以在此处获取更多信息:https: //www.codeigniter.com/user_guide/database/connecting.html

回答by Andre Dublin

It looks like your not sticking to the MVC pattern. You should be passing the data from view -> controller -> model.

看起来您没有坚持 MVC 模式。您应该从视图 -> 控制器 -> 模型传递数据。

As for sending information to the database, I'm pretty sure that CI handles xss and filter input, but you can never be to sure.

至于向数据库发送信息,我很确定 CI 处理 xss 和过滤器输入,但您永远无法确定。

Also make sure you are loading your models in the config/autoload.php file or initiate the model in the controller __construct() function

还要确保在 config/autoload.php 文件中加载模型或在控制器 __construct() 函数中启动模型

<?php
    class User extends CI_Controller
    {
        public __construct()
        {
            parent::__construct();
            $this->load->model('User_model');
        }
    }

or

或者

$autoload['model'] = array('User_model');

So for example in my login view, I would have the CI create the fields needed.

例如,在我的登录视图中,我会让 CI 创建所需的字段。

<?php 
    echo form_open('admin');
    echo form_label('Username:', 'username');
    echo form_input('username', 'name');
    echo form_label('Password:', 'password');
    echo form_password('password');
    echo form_submit('submit', 'Login', 'id="loginBtn"'); ?>
    echo form_close(); 
?>

Now in the controller

现在在控制器中

<?php
class User extends CI_Controller
{

    public function index()
    {

        $this->load->model('User_model');
        $result = $this
                    ->user_model
                    ->index(
                        $this->input->post('username'),
                        $this->input->post('password'));
    }

}
?>

And the model

和模型

<?php

class User_model extends CI_Model
{


    function index($username, $password)
    {
        $q = $this
                ->db
                ->where('username', $username)
                ->where('password', md5($password))
                ->limit(1)
                ->get('user');

        if ($q->num_rows() > 0) {
            return $q->row();
        }
    }
}

回答by Bogdan

num_rowsis a function, thus you need to add ()

num_rows是一个函数,因此您需要添加 ()

if($validate_user->num_rows() == 1) {
        return TRUE;
}

Also, are you calling the parent class constructor in the constructor

另外,您是否在构造函数中调用父类构造函数

class User_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
}