Inner Join not returning database values

Asked

Viewed 120 times

0

MY BANK

CREATE TABLE EXPERIENCIA(
     exp_pri INT NOT NULL AUTO_INCREMENT,
     nome VARCHAR(150),
     email VARCHAR(50),       
     exp VARCHAR(100),  
     PRIMARY KEY(exp_pri)
);

CREATE TABLE PRANCHA(
    prancha_pri INT NOT NULL AUTO_INCREMENT,    
    tamanho_prancha VARCHAR(8),
    meio_prancha VARCHAR(2),
    litragem_prancha VARCHAR(3),
    PRIMARY KEY (prancha_pri)
);

CREATE TABLE ALTURAPESOESTILO(
    idAltPes INT NOT NULL AUTO_INCREMENT,
    idExp INT,
    idPrancha INT,
    altura VARCHAR(4),
    peso VARCHAR(3),
    estilo VARCHAR(15),
    primary key (idAltPes),
    constraint fk_idExp foreign key (idExp) references EXPERIENCIA (exp_pri),
    constraint fk_idPrancha foreign key (idPrancha) references PRANCHA (prancha_pri)
 );

MY SQL:

$query = "SELECT EXP.exp, 
          AEP.altura, 
          AEP.peso, 
          AEP.estilo, 
          PRAN.tamanho_prancha, 
          PRAN.meio_prancha, 
          PRAN.litragem_prancha 
   FROM EXPERIENCIA AS EXP 
   INNER JOIN ALTURAPESOESTILO AS AEP 
   ON (EXP.exp_pri = AEP.idAltPes) 
   INNER JOIN PRANCHA AS PRAN 
   ON (PRAN.prancha_pri = AEP.idAltPes)";

 $resultado = mysqli_query($conexao,$query);

 $retorno = array();

 while($experiencia = mysqli_fetch_assoc($resultado)){
    $retorno[] = $experiencia;
 }

 return $retorno;

}

INDEX.PHP

 include 'banco.php';

    $resultado = array();
    $resultado = BuscaAlgo($conexao);


    foreach($resultado as $valor)
    {
        echo $valor['exp']; print(' '); echo $valor['altura']; print(' '); echo $valor['peso'];  
        echo $valor['estilo']; print(' '); echo $valor['tamanho_prancha']; print(' '); echo $valor['meio_prancha'];
        print(' '); echo $valor['litragem_prancha'];  ?><br> <?php  
    }

I am putting together three tables and trying to display them. However, when I put directly into Mysql and my code, nothing is returned. No error, but also nothing is displayed.

What can it be?

2 answers

1


The Inner Join only returns if the data is in both tables.

Try replacing your Inner Join with a left Join just to see what information is missing.

  • That’s exactly what it was! The problem was happening in filling out my foreign keys. Were returning as Null. I modified them and solved problem, thank you!

1

Guy in your php puts $result = mysql_query($connection,$query) or die(mysql_error()); So you will really see if the error is in the programming or in the query you have run.

Browser other questions tagged

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