M
M
Michael R.2016-02-24 10:04:49
PHP
Michael R., 2016-02-24 10:04:49

How to write a for loop correctly if we output data from MySQL?

Hello! There was a task to withdraw data from the database (MySQL) in a loop, I used the while loop, everything turned out:

while ($res = mysql_fetch_assoc($dbProject)) {
  echo '<pre>
    '.$res['id'].'
    '.$res['number'].'
    '.$res['status'].'
    '.$res['header'].'
    '.$res['description'].'
    '.$res['level'].'
    '.$res['customer'].'
    '.$res['prioritet'].'
  </pre>';
}

But then an additional task arose - to count the amount of output data, that is, it was necessary to use a counter. I took the for loop, wrote down all the data, but at one point a problem arose, namely:
for ($sum1=0; ?????? ; $sum1++) {
  echo '<pre>
    '.$res['id'].'
    '.$res['number'].'
    '.$res['status'].'
    '.$res['header'].'
    '.$res['description'].'
    '.$res['level'].'
    '.$res['customer'].'
    '.$res['prioritet'].'
  </pre>';
}

I understand that the place "??????" it is necessary to insert data from the database, but how to write them here correctly?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2016-02-24
@Mike_Ro

what prevents to count in the while loop ?

$rows = 0;
while ($res = mysql_fetch_assoc($dbProject)) {
    $rows++;
    // code
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question