2

I try to get some information out of my database to my webpage. Everything seems to be fine but there is one thing that doesn't want to go right. I put all my information out my database into $data. When i do this

print_r($data);

My webpage gives me this:

(
[0] => stdClass Object
   (
   [reparatie_id] => 19
   [customer_id] => 4
   [medewerker] => 4
   [name] => Joost
   )
)

Everything seems to be good but when i try to do this:

echo $data->voornaam;

I keep getting this error

A PHP Error was encountered


Severity: Notice

Message:  Trying to get property of non-object

Filename: reparaties/cases.php

Line Number: 7

Backtrace:

        File: C:\Ampps\www\beco\application\views\reparaties\cases.php

        Line: 7

        Function: _error_handler            


        File: C:\Ampps\www\beco\application\controllers\Reparaties.php

        Line: 57

        Function: view          


        File: C:\Ampps\www\beco\public\index.php

        Line: 315

        Function: require_once          
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 2
    I don't see `voornaam` property on your given data example? Also, it seems that your `$data` is an `array`, try doing `$data[0]->customer_id`? – Giedrius Jul 08 '17 at 19:22
  • `echo $data->voornaam;` need to be `echo $data[0]->name;` because no `voornaam` property is there in printed result – Alive to die - Anant Jul 08 '17 at 19:22
  • @Giedrius Thanx! You realy helped me out. I did not copied the whole output, thats the reason you didn't see 'voornaam' – J Tempelman Jul 08 '17 at 19:26
  • @JTempelman glad your issue is solved. `Alive to Die` has posted answer with same idea, can you please accept it as the answer to close this issue. – Giedrius Jul 08 '17 at 19:28
  • best answer of your question is here. [how-can-i-access-an-array-object](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – always-a-learner Jul 09 '17 at 04:44

3 Answers3

3

Since your $data is a single-dimensional-array,so it need to be-

$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2
Actually the $data array has an object at 0 position. So you need to any property of object. do like this:
<?php
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name; ?>
output will be:
19, 4, 4, joost
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
0

First of all, your $data array does not have the voornaam element in it. So, assuming you want to echo out the elements that are inside the array, you would use the foreach array, like this:

foreach($data as $value) {
    echo $value->name;
    echo $value->voorname; //if it exists
}

But, if you just want to access that single element from the array then you would do this:

echo $data[0]->name;
Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33