1

i am trying to update a progress bar with the iteration of a mysql query, and i can't understand how i can update the progress bar, and how i can found what number of row i have fetched, for example:

$query = 'SELECT tvshows.genres, tvshows.id_show FROM tvshows where tvshows.genres is not NULL';
$result = mysql_query($query);

$num_rows = mysql_num_rows($result);
echo $num_rows;

this: echo $num_rows; is the number of rows i have fetched, and then in this way i iterate the result:

while ($db_row = mysql_fetch_assoc($result)) 
{
    //Do seomthing with the row

}

but how i can know in what row i fetch to update then the progress bar? and anyone knows a good tutorial or sample code to do a progress bar? i have found this: http://w3shaman.com/article/php-progress-bar-script

but that example require these:

 for($i=1; $i<=$total; $i++){
// Calculate the percentation
$percent = intval($i/$total * 100)."%";

and i doesn't know how make it with the result of the php query, anyone can help me?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • you would need to make an ajax call to the script and return the data to the client to produce any sort of progress output. but with any type of progress display it will slow down the actual process. the query would probably be finished on most databases before you really needed any progress. EDIT: forgot you could flush the output to browser as the link described – StrikeForceZero Mar 08 '13 at 16:01
  • You didn't explain what your progress bar is for - a progress bar shows a percentage of a total amount- what is your total amount? What are you measuring? – inorganik Mar 08 '13 at 16:01
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Mar 08 '13 at 16:11
  • This has more to do with the client side (HTML, javascript; not PHP or MySQL) – Kermit Mar 08 '13 at 16:11
  • thank you for all the comment and the answer, there 27000 rows in that query so i need to see when each row is processed, in the while i make an update of the row if some condition are verified, so i need a progress bar to see at what point i'm, because after some time give me a timeout and the loading block, so i want understand if the while arrives at the end or if it's interrupted. Why you say i have forgot to use the flush? – Piero Mar 08 '13 at 16:22

1 Answers1

4

As mentioned in the comments, it should be an extemely slow query if you should have to use a progress bar.

If it is, you could just add a simple counter to your loop:

$i = 0;
while ($db_row = mysql_fetch_assoc($result)) 
{
    $i++;
    $percent = intval($i/$num_rows * 100)."%";

    //Do seomthing with the row

}

And then do as mentioned in the article.

Bjørne Malmanger
  • 1,457
  • 10
  • 11
  • late to the game but what does it mean when the percentage is not printing/echoing while the query is happening? all the percentages echo out at the end when the query and page is done loading. – leoarce Dec 15 '17 at 15:36
  • I'm not sure how your're testing this, but it might be related to output buffering? https://stackoverflow.com/questions/8882383/how-to-disable-output-buffering-in-php – Bjørne Malmanger Jan 22 '18 at 15:12