Limited Registration Number mysql_fetch_array

Asked

Viewed 146 times

1

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

However I found 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 to show me the first 20 results, just show me from 21 up, ie the code I have shows all the results, but I just want the results 21 up, how can I do this?

  • The snippet stack is used to run javascript, html and css, that is front-end, there is no reason to use "snippet stack" to put php, c++, java, c#, it will never work. Understand as a constructive criticism.

  • I didn’t get your comment.

  • 1

    You are using this https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/ in a number of your questions. The link has an explanation of the use of stacksnippet, if you just want to mark the code use the button that the icon looks like this { }.

  • Ah, I get it, thank you.

  • I’m gonna start wearing this {}

  • Why don’t you use the OFFSET on its own query?

  • 1

    Your example begins with mysqli and ends with mysql_fetch_array, mysqli_ is a new API andmysql_ is an old API, both do not talk.

Show 2 more comments

1 answer

1

There are two ways I can think now:

  1. 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 registrations from the number you defined earlier.

  1. 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 the OFFSET you must have defined a LIMIT previously, see in the http://dev.mysql.com/doc/refman/5.0/en/select.html. If you have a giant table, set the maximum of 18446744073709551615 in the LIMIT.

Browser other questions tagged

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