Trying to get property of non-object?

Trying to get property of non-object? in reference to // $thac0 = $data->records->row->strength;
the following function is in my Code-Igniter framework, but it keeps giving me an error message that $thac0 is trying to get the property of a non-object…
Can anyone spot my mistake?

function thac(){

$agrsr = trim($this->input->post('agrsr')) ;
$total = trim($this->input->post('total')) ;
$weapon = trim($this->input->post('weapon')) ;
$target = trim($this->input->post('target')) ;

$this->load->model('membership_model');
$data['records'] = $this->membership_model->getDfnsvTrgt($target);

$thac0 = $data->records->row->strength;

$array = array('total'=>$total, 'weapon'=>$weapon, 'target'=>$target, 'thac0'=>$thac0 );

echo json_encode($array);


}

I’m also including it’s call out to getDfnsvTrgt($target);

function getDfnsvTrgt($target)
	{
		$this->db->where('username', $target);
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			foreach ($query->result() as $row) {
			    $data[] = $row;
			}
		return $data;
		}
		
	}

$data[‘records’] = $this->membership_model->getDfnsvTrgt($target);

$thac0 = $data->records->row->strength;

You assign the result to an array. Then you try to use the array as an object. Next, the returned query result is an array as well which you try to use as a object.Truth be told I find the getDfnsvTrgt function confusing. I think something like this is what you want.


$thac0 = $data['records'][0]->strength;

Basically you should select a value (a row in this case)of the query result and that value probably is an object on which you can call the property.

Ah, thank you… now I understand. I’m still pretty new to the field. Thank you so much!

Could I exchange the explicit field name for a variable representing one of five fields I expect to be in the data base?

$thac0 = $data['records'][0]->$field_I_hope_to_find;

yes