Reputation: 1
I got the following message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/usermodel.php
Line Number: 4146
and here is my function where the line 4146 is:
else if($resultRes->average<'3' && $resultRes->average>='2')
This is the full function code. I have the same message for line 4137, 4140, 4143, 4146 and 4149
function get_Ranking($resid=''){
$sql="select ((Qrating+Srating+Drating+Crating)/4) as average from testimonial where RestId=".$resid;
$query=$this->db->query($sql);
$resultRes=$query->row();
if($resultRes->average == '5'){
return '050';
}
else if($resultRes->average<'5' && $resultRes->average>='4'){
return '040';
}
else if($resultRes->average<'4' && $resultRes->average>='3'){
return '030';
}
else if($resultRes->average<'3' && $resultRes->average>='2'){
return '020';
}
else if($resultRes->average<'2' && $resultRes->average>='1'){
return '010';
}
else{
return '000';
}
}
How can i fix it?
Upvotes: 0
Views: 15543
Reputation: 4197
It's because some of your queries are empty.
if($query->num_rows() != 0)
first
Upvotes: 3
Reputation: 1137
Try to print the $resultRes before getting average from it.
$resultRes=$query->row();
printr($resultRes);
also print the last query and check whether your parameters are correctly populated in the query using below query.
echo $this->db->last_query();
It can be the case your parameter is empty and no record retrieved from table.
Upvotes: 0
Reputation: 28763
Try with
if($resultRes[0]['average'] == '5'){
return '050';
}
else if($resultRes[0]['average'] < '5' && $resultRes[0]['average'] >= '4'){
return '040';
}
Since it will return an single row only,You can either use $resultRes['average']
directly.
Upvotes: 0