0

SOLVED: thanks to everyone for the fast answers

I have 3 tables, i joined them(checked in SQL and the values are right) but the problem is that 2 of them have the same field "denumire" with different values and i need to echo them

To be clearer this code is echoing the same value but in the mysql the ID's are right:

echo "<td valign='top'>" . nl2br( $row1[`specializare`.'denumire']) . "</td>";  
echo "<td valign='top'>" . nl2br( $row1[`disciplina`.'denumire']) . "</td>"; 

mysql tables structure:

specializare->ID_specializare, denumire 
disciplina->ID_disciplina,denumire 
preda->ID_specializare, ID_disciplina, etc

sql_query in php:

$sql1 = mysql_query("SELECT preda.ID_material, prof.ID_prof, prof.nume, prof.prenume, `specializare`.denumire, disciplina.denumire, preda.an_studiu, preda.semestru, preda.material
FROM `prof`
LEFT JOIN `preda` ON `prof`.ID_prof = `preda`.ID_prof
LEFT JOIN `specializare` ON `specializare`.ID_specializare= `preda`.ID_specializare
LEFT JOIN `disciplina` ON `disciplina`.ID_disciplina = `preda`.ID_disciplina
WHERE '".$_SESSION['ID_prof']."'=prof.ID_prof
ORDER BY specializare.denumire;"); 
2
  • take one of the fields that is a duplicate and use the AS syntax to give it a different name
    – dbf
    Sep 10, 2012 at 12:57
  • so SELECT specializare.denumire, disciplina.denumire as disciplina.denumire2 ?
    – Vlad
    Sep 10, 2012 at 13:04

5 Answers 5

5

When you run the SQL query, you should use an alias column name.

e.g.

SELECT t1.id as t1_id , t2.id as t2_id FROM table1 t1 , table2 t2 WHERE t2.id = t1.table_id_match
1

Use aliases in your query

SELECT specializare.denumire as val1, disciplina.denumire as val2 etc .....

then do

echo "<td valign='top'>" . nl2br( $row1['val1']) 
0
0

You should use an alias on your tables and columns like this:

select
    a.someColumnName as aCol,
    b.someColumnName as bCol
from
    table1 a
        join table2 b
            on a.tableID=b.tableID

And use the a/b (or whatever alias you set on tables) to determine which columns and how you refer to them when you bring them in (by using the as aCol) in the query.

0

When you select the column, you can an alias:

SELECT denumire AS specDenumire FROM specializare
0

Yes, here we need to understand wts happening.

In mysql, you might have overcome the conflict by tablename.columnname but the result given by mysql would be: denumire | denumire instead of specializare.'denumire' | disciplina.'denumire'

So, whem php stores that result in array while using mysql_fetch_assoc(). since, the key is like they are overwritten i.e $arr['denumire'] = 1 //specializare.'denumire' will be overwritten by $arr['denumire'] = 2 //disciplina.'denumire'. So, thus you solve by alias

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.