Skip to content
Advertisement

How can i echo out the helper_tbl status column seperately and the getter_tbl status column field too

I want to be able to specify the table column am echoing .

Am getting blank when i try to print out just from the helper column

$query = $db->fetch("SELECT n.*, g.* FROM helper AS n LEFT JOIN getter AS g ON n.user_id= g.user_id WHERE n.user_id='{$id}' OR g.user_id='{$id}' ");

if($query !== ''){
    foreach($query as $value){
        $output .= $value['n.status'];
    }
    print $output;
}

Advertisement

Answer

Your problem is that both columns are called status, so when the result array is formed the second status value overwrites the first (since a PHP array can only have one value for a given associative key). You can work around that using column aliases:

$query = $db->fetch("SELECT n.*, g.*, n.status as n_status, g.status as g_status
                     FROM helper AS n 
                     LEFT JOIN getter AS g ON n.user_id= g.user_id 
                     WHERE n.user_id='{$id}' OR g.user_id='{$id}' ");
if($query !== ''){
    foreach($query as $value){
        $output .= $value['n_status'];
    }
    print $output;
}

Note you will have the same issue with your user_id column (and possibly others). Generally it is better practice to enumerate the columns you want individually (rather than using *) so that you can avoid this problem from the beginning.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement