Welcome Guest, Not a member yet? Register   Sign In
Select as with non unique results
#1

[eluser]Ty Bex[/eluser]
I am joining 2 tables with columns that have the same name "NAME" and "ID"
The problem is I would expect to get the name and id from each product according to my select.

Is there a way to get the result of each column that I am selecting?

TABLE ecom_product
Code:
-----------------------------------------
|ID   | ACTIVE | NAME           | LEVEL |
-----------------------------------------
| 3   | 1      | Product 3 Name | 0     |
-----------------------------------------

TABLE ecom_level
Code:
-----------------
|ID | NAME      |
-----------------
| 0 | Senior    |
| 1 | Junior    |
-----------------

Code:
$this->db->select('p.id, p.active, p.name, l.name');
$this->db->from('ecom_product as p', FALSE);
$this->db->join('ecom_level as l', 'p.level = l.id', 'left');
$this->db->where('p.active', true);
$this->db->where('p.id', $id);
$this->db->limit(1);
$Q = $this->db->get();
if ($Q->num_rows() > 0){
    $row = $Q->row();
    print_r($row);
    exit;
    #return $row;
}

RESULT

stdClass Object ( [id] => 3 [active] => 1 [name] => Senior )
#2

[eluser]Ty Bex[/eluser]
Never mind.. Instead of being a lazy coder I fixed the database structure..
#3

[eluser]danmontgomery[/eluser]
Just to explain this (even though you found a different solution), even though you are differentiating between tables for the "name" column, the table name isn't included in the result, so you were getting back 2 "name" columns, and PHP doesn't handle non-unique array keys. The simple solution would be to just assign aliases for the result columns:

Code:
$this->db->select('p.id AS p_id, l.id AS l_id, p.active, p.name AS p_name, l.name AS l_name');




Theme © iAndrew 2016 - Forum software by © MyBB