Welcome Guest, Not a member yet? Register   Sign In
getting property of non-object error
#1

(This post was last modified: 04-27-2019, 03:00 AM by richb201.)

Severity: Notice --> Trying to get property of non-object



Here is the code






 

PHP Code:
function company_update($row)
{
 
   $userid=$this->session->userdata('userid');

 
   $query=$this->db->get_where('campaigns2',array('id'=>$row));

 
   $campaign $row->company_division;

 
 //  $campaign = $row->company_division;
 
   $this->session->userdata('campaign')-> $campaign;   << line with error
    $this
->db->set('campaign'$campaign);
 
   $this->db->where('email'$userid);
 
   $this->db->update('users'); 

This code seems to run fine on my windows apache and also on my AWS Lightsail server. Why would it be different on Ubuntu? I am thinking that I copied the campaigns2 table from the windows PC. Perhaps things got out of sync? Confused
proof that an old dog can learn new tricks
Reply
#2

The $row variable you're passing to the function, is a numeric value, right?

PHP Code:
$campaign $row->company_division
will throw the error, because on that line, $row suddenly is an object. But it's coming from nowhere, so there is no company_division property.

I think you have to change it to this:
PHP Code:
$query=$this->db->get_where('campaigns2',array('id'=>$row));
$campaign $query->row()->company_division
Reply
#3

(This post was last modified: 04-29-2019, 06:54 AM by richb201.)

$row is actually the auto-increment (index) field from mysql. I am using GroceryCrud so when a user clicks on a row in my grid, GroceryCrud is sending the auto-inc field to company_update(). Is it an int? No, in php it is a $string. The problem is that I only have the auto-inc field, so I need to query the campaigns2 table to actually get the "real data". So I would think that $query->company_division would point at the company_data field in the table. But it doesn't work like that. I need to do some call to $results to get the actual fields.  

So I added $q=$query->result();
and also $q[0]->company_division;

I am still having issues. What really bothers me is that this works fine under Windows and also under Linux on AWS. Unless, of course I found an additional bug.
proof that an old dog can learn new tricks
Reply
#4

How many results do you expect if you're pointing to an auto-increment field? Just one, right? Then, the result() function makes no sense. Just use row() to fetch the one and only record that can be found. Row() will return an object with the record, holding the fields as properties. Row_array() does the same, but then the result is an array instead of an object.

You don't describe what issue you still have. Where does it go wrong?
To do some kind of debugging, use print_r() to visualize the result from the query.
PHP Code:
echo '<pre>';
print_r($query->row());
echo 
'</pre>';
die(); 

You can do this in your controller or model.
Reply
#5

(This post was last modified: 04-29-2019, 01:33 PM by richb201.)

I changed the code to:
       $userid=$this->session->userdata('userid');
       $query=$this->db->get_where('campaigns2',array('id'=>$row));
       $q=$query->row();
       $campaign = $q->company_division;
       $this->session->userdata('campaign')-> $q->$campaign;    <<<<this is line 723


But I am still getting this error:
<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: controllers/Configure.php</p>
<p>Line Number: 723</p>
proof that an old dog can learn new tricks
Reply
#6

(This post was last modified: 04-29-2019, 01:49 PM by Wouter60.)

What's the purpose of this line (723)?
PHP Code:
$this->session->userdata('campaign')-> $q->$campaign

It does not assign or output a value.
If the statement has any value at all, nothing happens with it.
It would make sense if you would store a value into the session variable, e.g.:
PHP Code:
$this->session->campaign $campaign
Reply
#7

(This post was last modified: 04-29-2019, 11:15 PM by richb201.)

I keep session data in session->userdata(). It is just a structure where I keep data only for "this" user.
proof that an old dog can learn new tricks
Reply
#8

(This post was last modified: 04-29-2019, 10:43 PM by Wouter60.)

Everything in a session is only for "this" user, so "userdata()" is not a special place where data is stored.
$this->session->userdata() is only for backwards compatibility.
In previous versions of CI, you had to add data to the session like this:
PHP Code:
$this->session->set_userdata('some_name''some_value'); 

But from version 3.x you can do this:
PHP Code:
$this->session->some_name some_value

or even:
PHP Code:
$_SESSION['some_name'] = some_value

To retrieve data from the session, you can do the same thing:
PHP Code:
$some_name $this->session->userdata'some_name');
$some_name $this->session->some_name;  //this is called the magic getter!
$some_name $_SESSION['some_name']; 
all do the same.

It's explained here: https://www.codeigniter.com/userguide3/l...ssion-data
Reply




Theme © iAndrew 2016 - Forum software by © MyBB