Differentiate columns using inner join

1

I have this code:

  $getAlumno = mysqli_query($con,"SELECT * FROM alumnos INNER JOIN modulos ON alumnos.nivel=modulos.ID WHERE alumnos.ID='$login'") or die ('error al obtener datos de alumno');
  $row = mysqli_fetch_array($getAlumno,MYSQLI_ASSOC);

My problem is that in alumnos I have the field name that shows the username:

<?php echo $row['nombre'];?>

and my table of modules has the same column name:

<?php echo $row['nombre'];?>

Then when I want to show the student's name information, he sends me the name of the module. How can I differentiate this information?

    
asked by DoubleM 06.12.2017 в 08:04
source

2 answers

3

When you use two columns of different tables that have the same names, in the Select it is convenient to give them an alias:

$getAlumno = mysqli_query(
    $con,
    "SELECT *,
        modulos.nombre as nom_modulo,
        alumnos.nombre as nom_alumno 
    FROM alumnos 
    INNER JOIN modulos ON alumnos.nivel=modulos.ID
    WHERE alumnos.ID='$login'"
) or die ('error al obtener datos de alumno');

  $row = mysqli_fetch_array($getAlumno,MYSQLI_ASSOC);
    
answered by 06.12.2017 / 11:25
source
-1

Do you need all the fields of both tables? Because I would recommend that you add an alias to each field and thus avoid data conflict.

    
answered by 06.12.2017 в 22:59