Why get()->result()->row() Fails in CodeIgniter and How to Fix It
The error message "Trying to get property of non-object" is one of the most common runtime exceptions in PHP, particularly when working with legacy frameworks like CodeIgniter 3. It usually indicates that your code is attempting to access a property or method on an object that does not exist. In the specific case of database retrieval, this almost always points to a mismatch between your expectation of data and what the query actually returned.
Consider the function provided in your source material:
public function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->result()->row('name');
}
The intent here is clear: fetch the name associated with a specific asset code. However, the logic chain used to extract that value is fundamentally flawed. The error occurs because you are chaining methods on objects that may be null. Specifically, $this->db->get() returns an object containing the query result. If the database contains no record matching the provided $asset code, this method returns an empty result set object (often represented as stdClass) or, depending on the specific version and configuration, potentially nothing usable immediately after calling .result().
The critical failure point lies in the sequence $this->db->get()->result()->row('name'). The .result() method converts the query result into an array of objects. If no rows are found, this returns an empty array []. You cannot then call .row('name') on an array. Arrays do not have a row method; that belongs to the result object returned by get(). Even if .result() were skipped, calling .row('name') on a result set containing zero rows attempts to access index 0 of an empty collection, resulting in "non-object".
To resolve this, you must first verify that data exists before attempting to extract a single property. The most robust pattern in CodeIgniter 3 is to use the get() method's built-in .row() function directly on the query result object, but only after ensuring the query executed successfully. However, even simpler is to check if the result set is empty before accessing properties.
Here is a corrected implementation that handles the absence of data gracefully:
public function sban_name($asset)
{
$query = $this->db->get('asset_types', 1);
// Check if any rows were returned
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->name;
}
return null; // Or throw an exception, depending on your application requirements
}
In this approach, we first execute the query and store it in a variable. We then explicitly check num_rows(). If rows exist, we call .row() once to get the single object and access its name property. This prevents the "non-object" error because we never attempt to chain methods on an empty array or null value.
Alternatively, if you prefer a more concise approach using the query builder's chaining capabilities without intermediate variables, you can use:
public function sban_name($asset)
{
$row = $this->db->select('name')->from('asset_types')
->where('code', $asset)->get()->row();
return $row ? $row->name : null;
}
This version uses the ternary operator to safely check if $row is an object before accessing its name property. If the query returns no results, $row will be null or an empty object depending on your CI setup, and the code will return null instead of crashing.
It is important to note that in modern PHP development, specifically with Laravel, this specific pattern is less common due to different database abstraction layers. However, within the context of CodeIgniter 3, understanding the distinction between result() (which returns an array) and row() (which returns a single object) is crucial. Mixing these or chaining them incorrectly leads directly to the runtime errors you are encountering. Always ensure that your data access logic accounts for the possibility of missing records before trying to read specific fields from them.