Join two tables with PHP and retrieve the data in the same html select element

-1

I am trying to list in the same element select of html the results of the query of different tables, but I am not getting. Here's the code below so someone can tell me where I'm wrong.

<div class="form-group">
    <label>Selecione a turma:</label>
    <select name="id_disciplina_turma_facul">
        <?php
            $consulta = \MySql::conectar()->prepare("SELECT tb_turma_facul.id_turma_facul as turma,
                                                 tb_turma_facul.id_disciplina_turma_facul,

                                                tb_disciplina_facul.id_discip_facul,
                                                tb_disciplina_facul.nome_discip_facul as dnome,                                                   
                                                 tb_turma_facul.id_professor_turma_facul,
                                                 tb_professor_facul.nome_professor as pnome

                                                   FROM tb_turma_facul

                                          INNER JOIN (tb_disciplina_facul, tb_professor_facul)

                                          ON (tb_turma_facul.id_disciplina_turma_facul =
                                          tb_disciplina_facul.id_discip_facul 

                                          AND tb_turma_facul.id_professor_turma_facul =
                                          tb_professor_facul.cpf_professor)");


            $consulta->execute();
            $consulta = $consulta->fetchAll();
            foreach ($consulta as $key => $value) {
        ?>

            <option value="<?php echo $value['dnome'] ?>">
                <?php echo $value['pnome']; ?>
            </option>

        <?php } ?>

    </select>
</div><!--form-group-->

<div class="form-group">
    <input type="submit" name="acao" value="Matricular em turma">
</div><!--form-group-->

The columns of the tables are described below. The turma table has the foreign keys of the disciplina and professor tables.

Table columns tb_disciplina_facul :

  
  • id_discip_facul
  •   
  • nome_discip_facul
  •   
  • carga_horaria_discip_facul
  •   

Table columns tb_professor_facul :

  
  • nome_professor
  •   
  • endereco_professor
  •   
  • complemento_professor
  •   
  • cep_professor
  •   
  • bairro_professor
  •   
  • cidade_professor
  •   
  • estado_professor
  •   
  • telefone_professor
  •   
  • formacao_professor
  •   
  • titulacao_professor
  •   

Table columns tb_turma_facul :

  
  • id_turma_
  •   
  • id_disciplina_turma_facul
  •   
  • id_professor_turma_facul
  •   
    
asked by anonymous 30.12.2018 / 15:27

1 answer

0

If the columns described are faithful to what is in your database, you have two typos in your select: site_id is like site_id and > id_discip_facul is like faculty_identifier .

I made these adjustments and got the desired result.

SELECT 
  tb_turma_facul.id_turma_facul as turma,
  tb_turma_facul.id_disciplina_turma_facul,
  tb_disciplina_facul.id_discip_facul,
  tb_disciplina_facul.nome_discip_facul as dnome,                                                   
  tb_turma_facul.id_professor_turma_facul,
  tb_professor_facul.nome_professor as pnome
FROM tb_turma_facul
   INNER JOIN (tb_disciplina_facul, tb_professor_facul)
ON (tb_turma_facul.id_disciplina_turma_facul = tb_disciplina_facul.id_discip_facul 
AND tb_turma_facul.id_professor_turma_facul = tb_professor_facul.cpf_professor)

PHPalsohadsuccessintheresult.

    
31.12.2018 / 20:35