1

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

Ali Akbar
  • 39
  • 10

2 Answers2

2

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

devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    You will need to remove the double quotes around this in the query `"barang.kode_item"` as that will return the string `"barang.kode_item"` and not the data from the column! Without doing that your answer is incorrect – RiggsFolly Feb 03 '16 at 09:58
  • 1
    @RiggsFolly: noticed brother :) – devpro Feb 03 '16 at 10:02
  • 2
    The only acceptable punctuation around column and table names is the back tick `. – Jay Blanchard Feb 03 '16 at 14:11
1

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>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ajeet Kumar
  • 805
  • 1
  • 7
  • 26
  • You dont actually need the alias but it does no harm, so as you are closer than all the others UV – RiggsFolly Feb 03 '16 at 10:03
  • Thank you so much ! Works like a charm :) – Ali Akbar Feb 03 '16 at 10:28
  • 1
    Ok, I have removed the UpVote based on what you said in your comment. You are right, the reason for the OP to make this change was not well explained at all in this answer. – RiggsFolly Feb 03 '16 at 14:06
  • 1
    Why should the OP "change their query as below"? A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Feb 03 '16 at 14:09