Pull image from database

Asked

Viewed 35 times

2

I’m creating a website recipes and wanted to show the image that the user entered when registering. In my database I made two tables, one from the recipe and the other for the image file.

 CREATE TABLE Receita(
  idReceita INT NOT NULL auto_increment,
  nome_Receita VARCHAR(45) NOT NULL,
  preparo  TEXT NOT NULL,
  ingredientes text not null,
  usuario_idUsuario int,
  categoria_idCategoria int,
  dificuldade_idDificuldade int,
  PRIMARY KEY (idReceita)
); 


  cREATE TABLE `arquivo` (
  `id` int(11) NOT NULL,
  `arquivo` varchar(200) NOT NULL,
  `data` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

then I add recipe id as foreign key in the file table and when writing to the database by php use the last id function. I just don’t know how to do the list of the two tables to show the right image on my site.

<div class="col-sm-6" style="" class="img-rounded">
            <?php
            $sql3 = "select idreceita From Receita where nome_receita= '" . $receita . "';";
            $result3 = $CONEXAO->query($sql3);
            $sql_busca = "select from arquivo where receita_idreceita = '" . $result3 . "';";
            $mostrar = mysqli_query($CONEXAO, $sql_busca);
            $qtd_arquivos = mysqli_num_rows($mostrar);
            while($dados = mysqli_fetch_array($mostrar)){
               $arquivo = $dados['arquivo']; 
            ?>
            <img class="img-fluid col-md-2 img-thumbnail" src="upload/<?=$arquivo?>" width=500 height:300 />
            <?php }?>

</div>   

If you can help me

1 answer

1


I believe I can use the INNER JOIN:

SELECT *
FROM   Receita
       INNER JOIN arquivo
               ON arquivo.id = Receita.idReceita
WHERE  nome_receita = '" . $receita . "'; 

So in the end:

<div class="col-sm-6" style="" class="img-rounded">
            <?php
            $mostrar = mysqli_query($CONEXAO, "SELECT * FROM Receita INNER JOIN arquivo ON arquivo.id = Receita.idReceita WHERE nome_receita= '" . $receita . "';");
            while($dados = mysqli_fetch_array($mostrar)){
               $arquivo = $dados['arquivo']; 
            ?>
            <img class="img-fluid col-md-2 img-thumbnail" src="upload/<?=$arquivo?>" width=500 height:300 />
            <?php }?>

</div>   

If you want to make two SELECT distinct, as you are using, you can specify the name of the column/index of the array:

Instead of doing (using $result3):

$sql_busca = "select from arquivo where receita_idreceita = '" . $result3 . "';";

You must do (use $result3["idreceita"]):

$sql_busca = "select from arquivo where receita_idreceita = '" . $result3["idreceita"] . "';";

This way you are stating that the value of receita_idreceita is the $result3["idreceita"] which will be the idreceita. Specify only $result3 you are reporting an array.

Browser other questions tagged

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