While - Returns only one record

Asked

Viewed 277 times

0

Why my WHILE is displaying only one record (are two records!)

$resultnome = mysql_query("
select DIVU.DIVU_NM_DIVULGACAO, DIVU.DIVU_DS_LOGRADOURO, DIVU.DIVU_DS_NUMERO, DIVU.DIVU_DS_TELEFONES, DIVU.ID_PRESTADOR_DIVULGACAO, SERVI.SERV_NM_SERVICO, TISE.ID
from DIVULGACAO DIVU 
LEFT JOIN SERVICO SERVI ON DIVU.ID = SERVI.ID_DIVULGACAO
LEFT join TIPO_SERVICO TISE ON SERVI.ID_TIPO_SERVICO = TISE.ID
WHERE DIVU.DIVU_NM_DIVULGACAO = 'MeuTexto' AND TISE.ID = 3
") or die(mysql_error());

$rows = mysql_fetch_array($resultnome);
$nomeunidade = $rows['DIVU_NM_DIVULGACAO'];
$enderecounidade = $rows['DIVU_DS_LOGRADOURO'];
$numerounidade = $rows['DIVU_DS_NUMERO'];
$telefoneunidade = $rows['DIVU_DS_TELEFONES'];

echo '<strong>'.$nomeunidade.'</strong><br>';

while($rows = mysql_fetch_array($resultnome))
  {
    echo $rows['SERV_NM_SERVICO'];
  }

echo '<br><span class="glyphicon glyphicon-map-marker"></span> '.$enderecounidade.', '.$numerounidade.'<br>';
echo '<span class="glyphicon glyphicon-earphone"></span> Tel. '.$telefoneunidade.'<br>';
  • Could post a variable output $resultespecialidade? she returns in array ?

  • What you can do is return in object form and use the foreach to show.

  • Post the variable output $resultnome, post in the question that is easier to understand.

  • I improved the question... I made it more objective.

  • You have two mysql_fetch_array, one of them before the while. This first moves the pointer to the second record.

1 answer

4


The problem is that you are already using the function mysql_fetch_array before the repeat loop, that is, you have already obtained a record, and the while will start in the second. A simple solution is to use the do...while:

do {
    echo $rows['SERV_NM_SERVICO'];
} while ($rows = mysql_fetch_array($resultnome));

Note that it is only possible to use this solution because you are already getting the first result before the loop.

  • Thank you Oeslei. Already imagine what was the reason for the problem, but still was not seeing a solution... Thanks solved.

Browser other questions tagged

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