简体   繁体   中英

PHP Error Undefined property: stdClass::$barang in CodeIgniter 2

Hi I encounterd this error :

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$barang

Filename: views/dashboard_admin.php

Line Number: 112

I will show you my code first then I'll tell you my thought. :)

model :

function get_data_table(){
        $query_result = $this->db->query('SELECT "barang.kode_item", nama_item, nama_ruang, jml_item_kondisi, kondisi 
            FROM barang 
            INNER JOIN info_barang ON barang.kode_item = info_barang.kode_item 
            INNER JOIN (
                SELECT ruang.nama_ruang, campur_table.kode_item 
                FROM ruang 
                INNER JOIN rekap_isi_ruang AS campur_table ON campur_table.nomor_ruang = ruang.nomor_ruang) AS barang_campur 
            ON barang.kode_item = barang_campur.kode_item');
        return $query_result;
    }

view :

<?php foreach($result as $data_barang):?>
       <tr>
           <td><?php echo $data_barang->barang.kode_item;?></td>
           <td><?php echo $data_barang->nama_item;?></td>
           <td><?php echo $data_barang->nama_ruang;?></td>
           <td><?php echo $data_barang->jml_item_kondisi;?></td>
           <td><?php echo $data_barang->kondisi;?></td>
       </tr>
<?php endforeach;?>

In view, I just figured out that barang.kode_item be read as barang(concat)kode_item.. The variable cannot contains ' . ' but I don't know how to resolve this because my query must use that ' . ' to resolve the ambiguous error in mysql. Is there any way to manipulate it ?

By the way, i'm sorry if there's any words that doesn't look familiar to you. Thanks

There is no need to use table alias when you are using the column name on view.

This should be:

$data_barang->barang.kode_item;

Like this:

$data_barang->kode_item;

Original Line:

<td><?php echo $data_barang->kode_item;?></td>

Side note:

If you want to debug more than use var_dump() before foreach() loop you will get the idea what are you getting from database:

var_dump($result);

AS @RiggsFolly mentioned, you are using column kode_item as a string in your SELECT Statement, it will return you string not the column value you need to change it also as:

SELECT barang.kode_item, nama_item ... // Here no need to use quotes.

Also note that, copy from my answer's comments section:

The only acceptable punctuation around column and table names is the back tick `. – Jay Blanchard

Change your Query as below

$query_result = $this->db->query('SELECT barang.kode_item as barang_item, nama_item, nama_ruang, jml_item_kondisi, kondisi 
                FROM barang 
                INNER JOIN info_barang ON barang.kode_item = info_barang.kode_item 
                INNER JOIN (
                    SELECT ruang.nama_ruang, campur_table.kode_item 
                    FROM ruang 
                    INNER JOIN rekap_isi_ruang AS campur_table ON campur_table.nomor_ruang = ruang.nomor_ruang) AS barang_campur 
                ON barang.kode_item = barang_campur.kode_item');

And read as

<td><?php echo $data_barang->barang_item;?></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM