Stop database return - Mysql/PHP

Asked

Viewed 102 times

1

I have a problem to finish a project, I need to return the records of a database table, however, in some records, have a data stored differently, so I need to stop the execution. I haven’t been able to find the solution so far.

What I need is: Display as a result, how many records are in the status column 0, limiting to a maximum 6 records, if they have other records after 2 records with status 0, stop the execution.

Example:

Tabela: 11_07_2017_sala1 id | matricula_aluno | entrada | saida | status 1 | 0 | 6.5 | 7 | 0 2 | 0 | 7 | 7.5 | 0 3 | 2 | 7.5 | 8 | 1 4 | 2 | 8 | 8.5 | 1 5 | 2 | 8.5 | 9 | 1 6 | 0 | 9 | 9.5 | 0

When I Query with the clause WHERE status = 0, skipped the records that are with status = 1, but continued to display the results of other records.

I added LIMIT 6, however, the records continued to return all, with the exception of those in which the status = 1, limiting to 6 records.

What would be the correct form for this application?

Thanks!

  • Can you be clearer? I personally couldn’t understand what you want to do. If possible, give examples of the two situations: when to stop execution and when not to.

  • The problem is no limit. can you post your querie? I can imagine what it is!

  • Query with clause WHERE and LIMIT: SELECT * FROM 11_07_2017_sala1 WHERE status = 0 LIMIT 6 Can return more records if status = 0 Cannot return if next record status = 1

1 answer

1


Your query with the WHERE status = 0 clause will only return 0 values to status, so there is no chance of verifying the situation próximo registro o status = 1

Break is used to control structures like while, do while, for and switch.

The break function is to force the output of a structure. For example, if we have a repeating structure that goes up to a certain number, but should stop before any condition is met, in this case we use break.

In your case when there are two status 0 followed by a status não 0 OR seis zeros seguidos

//selecione sem where sem limit
SELECT * FROM 11_07_2017_sala1
$zeros=0;

..............................
..............................

    //condição para incrementar numero de status igual a 0
    if($status==0){
        $zeros=$zeros+1;

        //ação enquanto as condições não forem atendidas
        // EXEMPLO
        echo "<tr><td>".$id."</td><td>".$matricula_aluno."</td><td>".$entrada."</td><td>".$saida."</td><td>".$status."</td></tr>";

    }

    //aqui as condições para interromper a iteração
    if ((($zeros==2 && $status!=0))||($zeros==6)){
        break;
    }
  • It did not meet 100% of my need, but gave a light at the end of the tunnel. Many thanks @leo-Caracciolo

  • @Hugochristian what would be missing for the 100%?

  • It was just a matter of operators and even structure, as this was placed within a while, but the ideal was one of the while. But it’s already solved. I used it differently but with the same basis answered by you: if ((($status >= 1 && $StatusReturn != 0)) || ($status == 6 )) {&#xA; break;&#xA; } The other changes I made to your code, it was to fit my need

Browser other questions tagged

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