PHP While Generating Loop More

0

My while inventing an extra count. Why this?

$rsMsgLer = $conexao->query("SELECT * FROM mensagens WHERE para = '".$row['ID_Cadastro']."' GROUP BY de");

.

<?php do { ?>

...

<?php } while ($rowMsgLer = $rsMsgLer->fetch_assoc()); ?>

Only 3 records should appear:

butyouarelisting4,oneissomeerror:

    
asked by anonymous 27.07.2017 / 23:58

1 answer

3

The problem is to use the do ... while structure to iterate over the registry. Considering the code below:

do {

    <block>

} while (<condition>);

The code block set to <block> will be executed before the <condition> condition is checked. As in your problem, the condition is an assignment of the bank record to the variable $rowMsgLer , in the first iteration of this loop, this variable will not exist, generating the presented error. To fix, just switch to the while structure:

while (<condition>) {

    <block>

}

So, the condition will be checked before the code block is executed and therefore, already in the first iteration of the loop, the variable will be properly $rowMsgLer defined.

    
28.07.2017 / 00:10