Home > Net >  Trying to get property 'id_dimensiona' of non-object
Trying to get property 'id_dimensiona' of non-object

Time:12-31

i get error Trying to get property 'id_dimensiona' of non-object in C:\xampp\htdocs\admin\includes\forms\machinedecoupe.php on line 198 PHP 7.4 MariaDB 10.4.22

function 1

`

public static function find_by_id($id){ 
    global $database;
    
    $array_result=static::exec_query("SELECT * FROM ".static::$db_table." WHERE id=$id LIMIT 1");
    //Example : SELECT * FROM dimension WHERE id=669 LIMIT 1

    return  !empty($array_result) ? array_shift($array_result) : false;
}

`

function 2 :

`

foreach ($dims as $dim) {

     if(Lignedecoupe::find_by_dim_count($dim->id_dim,$dim->id_decoupe)!=0)
 { 
 
  $obj =  Dimension::find_by_id($dim->id_dim);
  echo print_r($obj->id_dimensiona); //error here showing data and error

 

  echo '   <tr >
        <td >'.Dimensiona::find_by_id(Dimension::find_by_id($dim->id_dim)->id_dimensiona)->ValeurA.'*'.Dimensionb::find_by_id(Dimension::find_by_id($dim->id_dim)->id_dimensionb)->ValeurB.'*'.Dimensionc::find_by_id(Dimension::find_by_id($dim->id_dim)->id_dimensionc)->ValeurC.'</td>
        <td >'.Decoupe::find_by_id($dim->id_decoupe)->type_decoupe.'</td> 
  
         <td ><input   type="number" min="0" name='.$k.' id='.$k.' value='.Lignedecoupe::find_by_dim_count($dim->id_dim,$dim->id_decoupe).'></input>
       </td>
           
  
        '.$message3[$k].'
   </tr> ';

 }

`

i get error Trying to get property 'id_dimensiona' of non-object in

print_r

Dimension Object ( [id] => 669 [id_dimensiona] => 2 [id_dimensionb] => 2 [id_dimensionc] => 4 ) 1 Dimension Object ( [id] => 624 [id_dimensiona] => 2 [id_dimensionb] => 2 [id_dimensionc] => 1 ) 1 Dimension Object ( [id] => 1 [id_dimensiona] => 1 [id_dimensionb] => 1 [id_dimensionc] => 1 ) 1 Dimension Object ( [id] => 768 [id_dimensiona] => 2 [id_dimensionb] => 5 [id_dimensionc] => 10 ) 1 Dimension Object ( [id] => 661 [id_dimensiona] => 3 [id_dimensionb] => 4 [id_dimensionc] => 3 ) 1 1

Notice: Trying to get property 'id_dimensiona' of non-object in C:\xampp\htdocs\admin\includes\forms\machinedecoupe.php on line 204

CodePudding user response:

As @Syscall pointed out, the error might have been triggered due to the false being returned.

Try wrapping your code like this:

  $obj =  Dimension::find_by_id($dim->id_dim);
  if(is_object($obj)){
    echo print_r($obj->id_dimensiona); //error here showing data and error
  }

Furthermore, if you want to handle if in case the output is not an object, attach an else statement as well.

if(is_object($obj)){
    echo print_r($obj->id_dimensiona); //error here showing data and error
  }
else{
  echo $obj;
}
  • Related