Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
Getting the same value while using foreach in PHP

I have a row named trailers in a MySQL database and I am querying that table using PHP and storing the result of that query in a variable. After that ,I am using a foreach loop to parse through each value of the variable i.e. each and every trailer value in the table.

But the problem is, when I try to echo the values of the variable only the first value of the variable is getting echoed in place of all other value. This is the query.

$query1 = "SELECT title,img,ratings,star_cast,director,trailer,imdb_ratings,lifetime_collecton,whats_good,whats_bad,watch_or_not 
    FROM recent_movies";

$result1 = mysqli_query($connect, $query1);

This is the code of the for each loop

<?php 
foreach($result1 as $r):
    echo $r['trailer'];
endforeach;
?>

This is how the output looks like

Ten same values are getting printed instead of 10 different values.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use the mysqli_fetch_array function to retrieve rows from a mysqli resultset.

Reference: http://php.net/manual/en/mysqli-result.fetch-array.php


 $result1 = mysqli_query($connect, $query1);
 while( $row = mysqli_fetch_array($result1,MYSQLI_ASSOC) ) {
     echo "<br> " . $row['title'] . " " . $row['ratings'] ;
 } 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda