Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

codeigniter - Trying to get property of non-object php variable

Hi everyone,

I'm facing an issue with CodeIgniter and I could really use some help. I'm getting the error message "Trying to get property of non-object" when trying to access a property from a PHP variable.

Here's some personal context to better explain my situation:

I'm currently building a web application using the CodeIgniter framework. In one of my controller functions, I'm querying the database and storing the result in a variable called $result. This variable is supposed to hold an object representing a specific database record.

Next, I'm trying to access a property of this object using the arrow operator (->) syntax. However, instead of getting the expected value, I'm encountering the "Trying to get property of non-object" error.

I've double-checked my code and made sure that the $result variable is not empty or null before attempting to access the property. I've also verified that the property I'm trying to access actually exists in the object.

Here's a simplified version of the code snippet I'm working with:

$result = $this->db->get_where('my_table', array('id' => 123))->row();

if (!empty($result)) {
$myProperty = $result->property;
// Rest of my code...
} else {
// Handle the case when no record is found

I would really appreciate it if someone could shed some light on why I'm encountering this error and how I can resolve it. It's been quite frustrating, and I've been unable to find a solution on my own.

Thanks in advance for any assistance you can provide!

All Replies


Hey folks,

I've encountered a similar problem in the past when working with CodeIgniter, so I thought I'd chime in with my experience. The "Trying to get property of non-object" error can be a bit frustrating, but there are a few things you can try to troubleshoot the issue.

Firstly, it might be worth checking the database query itself. There's a chance that the query didn't return any results, resulting in a null value being assigned to the $result variable. In such cases, attempting to access a property on a non-object will trigger the error you're seeing. To ensure there are no issues with the query, you could add some debug statements and verify the SQL being generated, as well as ensuring the expected data exists in the target table.

Another possibility is that the query executed successfully, but you're expecting a single row result, whereas the `row()` method is returning something unexpected. I suggest checking the CodeIgniter documentation for the `row()` method to ensure you're using it correctly.

Additionally, you could try using the `result()` method instead, which returns a result as an array of objects. Here's an example modification of your code using `result()`:

$query = $this->db->get_where('my_table', array('id' => 123));
$result = $query->result();

if (!empty($result)) {
$myProperty = $result[0]->property; // Access the property
// Rest of your code...
} else {
// Handle the case when no record is found

By using `result()` and accessing the first element of the result array, you can effectively access the property without encountering the error. Remember to check if the result array is empty before attempting to access the property.

I hope this helps you identify and resolve the issue. If you've tried these suggestions or need further assistance, please provide more information, and I'll be happy to assist. Good luck with your CodeIgniter project!


Hey there!

I've encountered a similar issue before when using CodeIgniter, and hopefully, I can offer some insight. The "Trying to get property of non-object" error message usually occurs when you're trying to access a property of a variable that is not an object.

In your case, I suggest adding some additional checks to ensure that the $result variable is indeed an object before accessing its properties. This step is essential to prevent the error from occurring.

You can try using the `is_object()` function to validate the $result variable. Here's an updated version of your code snippet:

$result = $this->db->get_where('my_table', array('id' => 123))->row();

if (is_object($result)) {
$myProperty = $result->property;
// Rest of your code...
} else {
// Handle the case when no record is found or $result is not an object

By adding the `is_object()` check, you can ensure that you're only accessing the property if $result is indeed an object. This way, you won't encounter the "Trying to get property of non-object" error.

Give it a try and let me know if it resolves your issue. If not, feel free to provide more details and code snippets, and I'll do my best to help you further.

Good luck!

New to LearnPHP.org Community?

Join the community