What am I doing wrong in this PHP MySQL exercise [closed]

0

I'm doing an inner join of 3 tables and it does not work ... with 2 tables it's all good .. when I add the third table it does not show me anything.

 <?php 
        try{
            require_once('include/funciones/db_conexion.php');
            $sql = "SELECT 'evento_id', 'nombre_evento', 'fecha_evento', 'hora_evento', 'nombre_invitado'";
            $sql .= "FROM 'eventos'";
            $sql .= "INNER JOIN 'categoria_evento'";
            $sql .= "ON eventos.id_cat_evento=categoria_evento.id_categoria";
            $sql .= "INNER JOIN 'invitados'";
            $sql .= "ON eventos.id_inv=invitados.invitado_id";
            $resultado = $conn->query($sql);
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
  ?>
  <?php while($eventos = $resultado->fetch_all(MYSQLI_ASSOC) ){
    ?>
    <pre>
    <?php var_dump($eventos); ?>
  </pre>
  <?php } ?>
    
asked by natalia palvacino 18.10.2018 в 03:10
source

1 answer

1

My suspicion is that by concatenating like this you are not leaving spaces between the keywords, so that the final SQL is:

SELECT 'evento_id', 'nombre_evento', 'fecha_evento', 
'hora_evento', 'nombre_invitado'FROM 
'eventos'INNER JOIN 'categoria_evento'ON 
eventos.id_cat_evento=categoria_evento.id_categoriaINNER JOIN 
'invitados'ON eventos.id_inv=invitados.invitado_id

It works fine by putting only the first JOIN because it is in the merging of the condition of ON of the first JOIN (in the name of the field) and the keyword INNER that everything collapses slowly. The first JOIN works because your code is as follows:

SELECT 'evento_id', 'nombre_evento', 'fecha_evento', 
'hora_evento', 'nombre_invitado'FROM 
'eventos'INNER JOIN 'categoria_evento'ON 
eventos.id_cat_evento=categoria_evento.id_categoria

You must be careful with spaces when concatenating commands like this. Modify the code like this:

<?php 
        try{
            require_once('include/funciones/db_conexion.php');
            $sql = "SELECT 'evento_id', 'nombre_evento', 'fecha_evento', 'hora_evento', 'nombre_invitado' ";
            $sql .= "FROM 'eventos' ";
            $sql .= "INNER JOIN 'categoria_evento' ";
            $sql .= "ON eventos.id_cat_evento=categoria_evento.id_categoria ";
            $sql .= "INNER JOIN 'invitados' ";
            $sql .= "ON eventos.id_inv=invitados.invitado_id ";
            $resultado = $conn->query($sql);
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
  ?>
  <?php while($eventos = $resultado->fetch_all(MYSQLI_ASSOC) ){
    ?>
    <pre>
    <?php var_dump($eventos); ?>
  </pre>
  <?php } ?>
    
answered by 18.10.2018 в 03:47