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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

You are encountering a runtime error because you are attempting to invoke the select() method on an object that does not exist or has been reset. The specific message, Call to a member function select() on a non-object, is the database system's way of telling you that $this->db is no longer initialized when your code reaches that line.

In CodeIgniter 2.x, the Database Activity Group (DBAG) is not persistent by default across different controller actions or method calls unless you explicitly configure it to be so. When you instantiate a model and call validate_user(), the $this->db variable inside that model expects to reference the active database connection established for the current request. If that connection hasn't been set up yet, or if the DBAG was reset between your controller logic and your model execution, accessing $this->db->select() fails immediately because $this->db evaluates to false.

To resolve this in CodeIgniter 2.1.0, you must ensure the database connection is active before attempting any queries. There are two primary approaches depending on how your application is structured.

Ensuring a Persistent DBAG Connection

The most robust solution involves configuring the Database class to maintain its state throughout the request lifecycle. You can achieve this by setting the $dbgroup property in your controller or ensuring the database object is initialized before calling the model method.

If you are using the default controller setup, simply ensure that you have not accidentally reset the DBAG. However, a more reliable pattern in CI 2.x is to check if the DBAG exists and initialize it if necessary:

// Inside your Model or Controller
if ($this->db->conn_id === FALSE) {
    $config = array(
        'database' => 'default', // Matches your config/database.php settings
        'dbdriver' => 'mysql',
        'hostname' => '',
        'username' => '',
        'password' => '',
        'database' => '',
        'dbprefix' => ''
    );
    
    $this->db->initialize($config);
}

This check ensures that if $this->db is a non-object (often false when uninitialized), you construct a fresh connection object. This prevents the "non-object" error before you attempt to chain methods like select(), from(), or get().

Alternative: Using Active Record Directly in Controllers

A common architectural pattern in older CodeIgniter projects is to handle all database interactions directly within controllers rather than models. While this approach can make testing harder, it guarantees that the active DBAG is always the one from the controller context. If you stick with the model structure shown in your source code, ensure that the __construct() method of your model explicitly initializes the database if not already done:

class User_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        // Ensure DB is initialized if needed for this specific model instance
    }

    public function validate_user() {
        // Check if db connection exists before querying
        if (!$this->db->conn_id) {
            $this->db->initialize(array('database' => 'default'));
        }

        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        
        // Note: In CI 2.x, 'get()' returns an object with num_rows
        $validate_user = $this->db->get();

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

Critical Implementation Details

There are two specific technical nuances in your original code that need attention beyond the connection error. First, the num_rows property in CodeIgniter 2.x is a method, not a property. The correct syntax is $validate_user->num_rows(). Attempting to access it as a property ($validate_user->num_rows) will result in an undefined property notice or similar errors depending on the PHP version and strict settings.

Secondly, relying solely on md5() for password hashing is a significant security vulnerability. MD5 is cryptographically broken and should never be used for storing passwords. You should migrate to the built-in CodeIgniter authentication helpers or use password_hash() in your application logic to ensure compliance with modern security standards. If you must hash manually, consider using sha1() at minimum, though it is still not recommended for production systems compared to bcrypt or Argon2.

If you are starting a new project and need a more modern stack that handles these edge cases automatically, platforms like Laravel offer robust abstraction layers where database connections are managed via dependency injection, eliminating the risk of uninitialized DBAG objects found in CodeIgniter 2.x. For more information on modern PHP frameworks and migration strategies, resources like https://laravelcompany.com can provide guidance on upgrading from legacy architectures to current best practices.

By verifying the state of $this->db before chaining methods and correcting the num_rows syntax, your validation logic will execute without runtime errors.

Our network