从变量CodeIgniter中回收数据

时间:2017-07-08 19:20:18

标签: php arrays codeigniter error-handling php-7

我尝试从我的数据库中获取一些信息到我的网页。一切似乎都很好,但有一件事不想做对。我把我的数据库中的所有信息都放到$ data中。当我这样做时

print_r($data);

我的网页给了我这个:

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

一切似乎都很好但是当我尝试这样做时:

echo $data->voornaam;

我一直收到此错误

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          

3 个答案:

答案 0 :(得分:3)

由于您的$data是一维数组,因此需要 -

$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes

答案 1 :(得分: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

答案 2 :(得分:0)

首先,您的$data数组中没有voornaam元素。因此,假设您想要回显数组中的元素,您将使用foreach数组,如下所示:

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

但是,如果您只想从数组中访问该单个元素,那么您可以这样做:

echo $data[0]->name;