Read data in a SQL with columns of equal names in different tables

1

I need to retrieve data from 2 tables. My SQL shown below works perfectly. The problem is that I made a foreach to retrieve the data from the second table by the ID field that is conflicting with the first table since both have the ID field.

SQL

$sql = "SELECT tb_faqs.*, tb_paginas.* 
FROM (tb_faqs INNER JOIN tb_paginas) 
WHERE tb_faqs.ID = '$id' 
AND tb_faqs.id_pagina = tb_paginas.ID";

$query = $pdo->query($sql);
$pagina = $query->fetch();
This SQL query fetches all data needed for both tables but I need to foreach in the second table to mount a dynamic% . This is select :

foreach ($pdo->query($sql) as $pagina) {
     echo '<option value="'.$pagina['ID'].'" '.(($pagina['id_pagina'] == $pagina['ID']) ? 'selected="selected"' : "").'>'.$pagina['pagina'].'</option>';
}

Question: When two tables have the same column name , how to specify in which table that column?

    
asked by anonymous 14.11.2015 / 00:53

1 answer

2

Change your SQL to:

$sql = "SELECT tb_faqs.*, tb_fags.id as id_fag, tb_paginas.*, tb_paginas.id as id_pagina 
FROM (tb_faqs INNER JOIN tb_paginas) 
WHERE tb_faqs.ID = '$id' 
AND tb_faqs.id_pagina = tb_paginas.ID";

And at the time of Foreach () do: echo $ id_fag, echo $ id_page ... There is no need to be exactly these names, if it conflicts, change, it creates a smoothness for each ID.

    
14.11.2015 / 00:55