How to make a "while" of the number of rows of a query?

Asked

Viewed 726 times

1

Does anyone know if in that PHP function - mysql_num_rows has to do with me making a while in it? I need to break the total number of rows in the table. I tried to do:

$sql = "select ...";
$query = mysql_qery($sql);
$rows = mysql_num_rows($query);

while(!empty($rows)){

}

I tried the !isset, variable = "", variable = 0 but it makes an infinite loop and only takes the first crease query block lines, also tried to make a for but it didn’t work either.

I’ve tested with echo the screen return variable.

Keep making an infinite loop, I need this loop to take a range of lines I declared two variables, one for the

$inicio = 0;

and the other for the limit

$limit = 100;

range would be the block between start and limit and while loop incrementing the value of limit in offset, I tried to do:

$offset = $offset+$limit; 

and

$offset = $offset+100;

the more he takes only the first lines and makes the infinite loop on that first line.

Does anyone have an idea how to fix this?

i wanted to change the $offset variable every time I loop the value would be the value of the $limit variable that will be fixed has to do this dynamically in php

  • By the function mysql_num_rows, you have already checked if you are returning the right count?

  • First, give an echo $Rows. If returned right value, change your while: while($Rows > 0){

  • What you want is a for, probably and not a while. Now, it seems that it is the case to use LIMIT SQL will use only part of the lines. If you want to group the rows from 10 to 10, for example, or by headers/footers to each N records, already have answers on the site about this. If you are going to use for paging, you also have.

  • 1

    @daniloeugenio I believe I have finally understood your question from your other question (which is a duplicate and you should remove). In it you explained a little better about what you wanted. See if my answer below answers the question.

  • @Danilo, as I commented in your new question, you better [Dit] this one and explain the problem very clearly, instead of opening new questions about the same problem. Try to give as much detail of the result you want to get in the question itself, which makes it easier for the community to help.

1 answer

2

Do the following:

$query = mysql_query('SELECT * FROM tabela');
$linhas_afetadas = mysql_num_rows($query);
$limit = 100;

for ($offset = 0; $offset < $linhas_afetadas; $offset += $limit) {
    $query = mysql_query('SELECT * FROM tabela LIMIT ' . $limit . ' OFFSET ' . $offset);
    while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
        // $row é um registro da sua tabela
    }
}

In the given example, you will get the DB records from 100 in 100, iterating on these 100 inside while ($row ...) {}.

$row is an array, which will contain a record returned by your query, by iteration. Each array index will correspond to a column of your table. If your table is:

+----+----------+-------+-------+
| id | nome     | idade | grupo |
+----+----------+-------+-------+

So $row will contain the following structure at each iteration:

Array
(
    [0] => ID
    [1] => NOME
    [2] => IDADE
    [3] => GRUPO
)

Browser other questions tagged

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