Limited Registration Number mysql_fetch_array

1

Well, recently I created a question here in stackoverflow where I asked, how would I collect rows from the database with the criteria I wanted.

However I discovered that I could use mysql_fetch_array to collect these results and they gave me the following code:

$query = "SELECT...";
$sql = mysqli_query($conexao, $query);

while($dado = mysql_fetch_array($sql)){ // Enquanto houver dados ficará em loop
   $a = $dado['coluna1']; //recupera o dado do array
   $b = $dado['coluna2'];
   echo $a."-".$b."<br><br>"; //exibe o dado
} 

I would like to know, how could I do, not show me the first 20 results, only show me from the 21 up, ie the code I have shows all the results, but I just want the results 21 How can I do that?

    
asked by anonymous 27.03.2016 / 07:08

1 answer

1

There are two ways, I can think of now:

  • Using exactly what you have:

    $contador = 0;
    // Define um contador (para que cada loop +1)
    
    $limitador = 20;
    // Define um limite (fixo)
    
    while($dado = mysqli_fetch_array($sql)){
    
       if($contador >= $limitador){
       // Se for maior ou igual ao limite irá exibir os dados
    
       $a = $dado['coluna1'];
       $b = $dado['coluna2'];
       echo $a."-".$b."<br><br>";
       }else{
       $contador++; // +1 ao contador
       }
    
    } 
    
  •   

    The if will check whether or not you are with the records from the number you defined previously.

  • Changing the query:

    $query = "SELECT * FROM tabela WHERE qualquer = 'coisa' LIMIT 999 OFFSET 20";
    $sql = mysqli_query($conexao, $query);
    
  •   

    The offset will "ignore" the previous data and only display from it, without any change in your code.

    NOTE:

      

    To use OFFSET you must have set a LIMIT previously, see the manual at link .   If you have a giant table, set the maximum of 18446744073709551615 to LIMIT .

        
    27.03.2016 / 15:50