CODEIGNITER - Message: Trying to get property of non-object - php

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?

It's because some of your queries are empty.
if($query->num_rows() != 0)
first

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.

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.

Related

PHP Notice: Trying to access array offset on value of type bool in

I get the following error in PHP 7.4
Trying to access array offset on value of type bool in
When running this function
function check_admin($user)
{
$is_admin = mssql_fetch_array(mssql_query("Select * from Accounts where [Name] = '".$user."'"));
array();
if($is_admin['name'] != false){
if($is_admin['ip'] != ip($user)){
return false;
}
else{
return array($is_admin['name'],$is_admin['gm_level'],$is_admin['ip']);
}
}
else{
return false;
}
}
Any idea how to fix it?
Please check if the number of returned rows are greater than zero.
function check_admin($user)
{
$is_admin = mssql_fetch_array(mssql_query("Select * from Accounts where [Name] = '".$user."'"));
//checks if the number of rows returned are greater than zero.
if(mssql_num_rows($is_admin) > 0){
if($is_admin['name'] != false){
if($is_admin['ip'] != ip($user)){
return false;
}
else{
return array($is_admin['name'], $is_admin['gm_level'], $is_admin['ip']);
}
}
else{
return false;
}
}else{
return false;
}
}
I hope that helps you.
Upgrading PHP Fusion from 9.0 to 9.03.50 resolved the issue.

get rid of error before results show up with php

the below script throws an error before the results the script executed. Though i get the results i want but i want to get rid of the error that throws up.
When the results equals to 1 the error doesn't show up, but when its more than 1 the error comes.
Notice: Trying to get property of non-object in
C:\xampp\htdocs\server_scripts\complains_list.php on line 1
if(count($customers->User_Name)=='1') {
echo json_encode(array($data));
}else
{
echo json_encode($data);
}
You just need to check that variable is set or not, check manual
if(isset($customers->User_Name) && count($customers->User_Name)=='1')
{
echo json_encode(array($data));
}
else
{
echo json_encode($data);
}
I guess that at some point your $customer isn't instantiated. Thus you have to test for its existence :
if($customer != null && count($customers->User_Name)=='1') {
echo json_encode(array($data));
} else {
echo json_encode($data);
}

SugarCRM v6.5 anything after $bean->save stops working

I have this code:
function saveField($field, $id, $module, $value)
{
$bean = BeanFactory::getBean($module, $id);
if (is_object($bean) && $bean->id != "") {
if ($bean->field_defs[$field]['type'] == "multienum") {
$bean->$field = encodeMultienumValue($value);
}else if ($bean->field_defs[$field]['type'] == "relate" || $bean->field_defs[$field]['type'] == 'parent'){
$save_field = $bean->field_defs[$field]['id_name'];
$bean->$save_field = $value;
if ($bean->field_defs[$field]['type'] == 'parent') {
$bean->parent_type = $_REQUEST['parent_type'];
$bean->fill_in_additional_parent_fields(); // get up to date parent info as need it to display name
}
}else{
$bean->$field = $value;
}
//return here will work
$bean->save(); //this works
//nothing works here
return getDisplayValue($bean, $field);
} else {
return false;
}
}
The problem here is that anything under
$bean->save()
will not work. But I know that save is working as the values are being updated. So how can I debug this problem?
I already tried:
return var_dump($bean->save());
return print_r($bean->save());
if($bean->save()){
return "1";
}else{
return "2";
}
And none of those in the above worked I still get nothing in my return.
There is likely something such as an after_save logic hook that is executing and either causing a fatal error or doing an exit.
Try using xdebug, it should allow you to investigate further into the save method that fails.

How to check if php mysqli_fetch_array is empty before while loop

I wanted to make sure there are results before running the while loop but all the methods I am trying seem to remove the first result.
$nearbyResult = mysqli_query($con,$sqlNearby);
if(mysqli_fetch_array($nearbyResult) == 0) {
echo '<p>No results found, Add your property here.</p>';
} else {
while($rowNearby = mysqli_fetch_array($nearbyResult)) {
}
}
This line will take the first row of your result set and chuck it in the bin:
if(mysqli_fetch_array($nearbyResult) == 0) {
Change to:
if( ! mysqli_num_rows($nearbyResult) ) {
And check your freakin function returns:
if( ! $nearbyResult = mysqli_query($con,$sqlNearby) ) {
echo "Mysql error: " . mysqli_error($con);
}
You can use the mysql_row_count method to count how many rows are returned in your query http://php.net/manual/en/mysqli-result.num-rows.php
Try assigning the results to a variable as part of your if statement. If mysqli_fetch_array() has no result set to work with it will return false.
if($rowNearby = mysqli_fetch_array($nearbyResult)) {
//There was a result, work with it here
doStuffWith($rowNearby);
} else {
//No records in your result set, handle as desired
}

$this->db->_error_number() returning 0 and not the error number

Codeigniter not returning the error number
$this->db->trans_start();
$deal_enquiry = $this->db->insert('deal_enquiry', $data);
$enquiry_id = $this->db->insert_id();
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
echo $this->db->_error_number();
if ($this->db->_error_number() == 0){
$errormsg="Duplicate entry";
return $errormsg;
} else{
$errormsg="Enquiry failed";
return $errormsg;
}
// return false;
} else {
return true;
}
The error number is always returning 0 even if there is an error. When i set the db_debug to true the error number is something different. Why am i not getting the correct error number. Can someone please point out the problem.
Thanks,
I got the error number by echo $this->db->_error_number(); before the trans_complete();
Thanks all
For others experiencing similar issues. Make sure you have Database Debugging enabled in your database.php configuration file.

Categories