Make Join with table columns where id equals variable

Asked

Viewed 98 times

0

I have 4 tables they are already with Foreign key.

aluno:
id
matricula
username
curso_id          // referencia a id_cursos da tabela cursos, coluna 
id_cursos.

cursos: 
id_cursos
nome_cursos

modulos:
id_modulos
nome_modulos
cursos_id         //faz referencia a tabela cursos, coluna id_cursos.

materias:
id_materias
nome_materias
modulos_id       // faz referencia a tabela modulos, coluna id_modulos.

I need to return the 1 user columns:

    <?php
    function get_user_data(){
      $user = $_SESSION["Login"];
      $pass = $_SESSION["Password"];
      $pdo = cnx();
        $select = $pdo->query("select * from aluno where matricula = '$user' and password ='$pass' ");
        $select->execute();
        $rowCount = $select->rowCount();
        if ($rowCount == 0):
            header("Location:index.php?out=1");
        else:
            return $select;
        endif;
    }

    function stats_session(){
      session_start();
      if (isset($_SESSION['Login']) and isset($_SESSION['Password'])) {
        $data = get_user_data();
        $data->execute();
        foreach ($data as $key) {
          $curso_id = $key['id_curso'];
          $pdo = cnx();
          $select = $pdo->query("SELECT C.*, A.*, MO.*, MA.* FROM aluno A JOIN cursos C on C.id_cursos = '$curso_id' JOIN modulos MO on MO.id_modulos = '$curso_id' JOIN materias MA on MA.id_materias = MO.id_modulos");
          $select->execute();
          return $select;
        }
      } else {
        header('Location:index.php');
      }
    }
    $query = stats_session();
    $query->execute();
    foreach ($query as $key) {
        var_dump(json_encode($key));
    }
    ?>

is returning this:

C:\wamp64\www\frequencia.php:6:string '{"id_cursos":"1","0":"1","nome_curso":"Assistente Administrativo","1":"Assistente Administrativo","id":"1","2":"1","matricula":"1234","3":"1234","password":"1234","4":"1234","username":"2626","5":"2626","id_curso":"1","6":"1","id_modulos":"1","7":"1","nome_modulos":"modulo basico","8":"modulo basico","cursos_id":"1","9":"1","id_materias":"1","10":"1","nome_materias":"Introdu\u00e7\u00e3o a adm","11":"Introdu\u00e7\u00e3o a adm","modulos_id":"1","12":"1"}' (length=458)

C:\wamp64\www\frequencia.php:6:string '{"id_cursos":"1","0":"1","nome_curso":"Assistente Administrativo","1":"Assistente Administrativo","id":"2","2":"2","matricula":"4321","3":"4321","password":"4321","4":"4321","username":"Usu\u00e1rio 2","5":"Usu\u00e1rio 2","id_curso":"2","6":"2","id_modulos":"1","7":"1","nome_modulos":"modulo basico","8":"modulo basico","cursos_id":"1","9":"1","id_materias":"1","10":"1","nome_materias":"Introdu\u00e7\u00e3o a adm","11":"Introdu\u00e7\u00e3o a adm","modulos_id":"1","12":"1"}' (length=478)

Only the first one is correct, is returning the data of the 2 users I have in the student table, how do I return only what is in the variable $curso_id of the table students column curso_id ?

  • Only the first one is correct, is returning the data of the 2 users I have in the student table, how do I return only what is in the variable $curso_id of the table students column curso_id ? This question is confused. What is the first one that is working? And what is the second one that is not working? I posted an answer with an interpretation that may not be the right one

1 answer

1


Problem in sql query of the function below:

function stats_session(){
      session_start();
      if (isset($_SESSION['Login']) and isset($_SESSION['Password'])) {
        $data = get_user_data();
        $data->execute();
        foreach ($data as $key) {
          $curso_id = $key['id_curso'];
          $pdo = cnx();
          $select = $pdo->query("SELECT C.*, A.*, MO.*, MA.* 
FROM aluno A JOIN cursos C on C.id_cursos = A.curso_id and A.curso_id = '$curso_id' 
JOIN modulos MO on MO.id_modulos = '$curso_id' 
JOIN materias MA on MA.id_materias = MO.id_modulos");
          $select->execute();
          return $select;
        }
      } else {
        header('Location:index.php');
      }
    }
}

As can be seen, only the section has been changed C.id_cursos = A.curso_id and A.curso_id = '$curso_id' to relate the student and course tables, and filter through the foreign key present in student.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.