Hi,

I was wondering how to go about creating a progress bar for a mysql insert. Basically I insert a row into a mysql database, while this row is being inserted, a progress bar appears that checks the insert periodically untill the insert is complete, once the insert is complete, it redirects the user to a specific page. I have found many examples of using progress bars, but none that perform this function. Any help would be greatly appreciated. Thanks.

    saco721;11041585 wrote:

    I have found many examples of using progress bars, but none that perform this function. Any help would be greatly appreciated. Thanks.

    That'd be because if you have enough time to show a progress bar during an INSERT statement, you need to re-write said statement.

    Are you sure you don't want to show a progress bar while a PHP script is running? (Perhaps it's doing 10K/100K/1M INSERTs ...) ?

      Hi Dalecosp,

      Thanks for your reply!.

      How would I show a progress bar while the PHP script is running?, I could post the script I am running if that would help, its a bit of a mess though.

        6 days later
        saco721;11041609 wrote:

        How would I show a progress bar while the PHP script is running?

        Show a progress bar where? If it's in a browser, you'd either have to handle the progress bar in the browser, or you'd have to disable the web servers output buffer (if possible). I'd recommend doing it all in the browser.

        You'd also need a way to calculate the total "size" of the progress bar. Doing it apple "migration guide" style is a bad idea. Because if the estimate is so bad that the progress bar will show "less than one minute remaining" for an hour or two, there's a good chance I will turn off my computer.
        If you are inserting 100k rows of static data / pre-computed data, you may probably assume that every 10k rows takes ~10% of the total time. But if it takes just a little bit longer to compute the data for each next row, then the last 20k rows might take as much time as the first 80k rows.

        Other than that, all you have to do is check progress and compare it to expected total to get the completion ratio , and then fill a progress bar accordingly.

        A good first step would be to create a progress bar which fills up by 10% each second for 10 seconds. Then you extend it to take an integer input signifying the total time it should take to fill the progress bar and update it every second. Then you extend it to update at non-second intervals. Finally you use this together with your actual work load.

          Inserting a row into a database might take a millisecond -- or ten milliseconds. Setting up a progress bar is going to require you to write at least one other PHP script which can somehow check on the status of this insert and then probably some client-side Javascript to repeatedly check that second PHP script. In other words, this is probably not worth it for a single row insert. If you are sticking in a ton of data or some other time-consuming process, you should be careful of PHP scripts timing out (PHP.ini has some default timeout values that limit script execution to some number of seconds, for example). If you are performing some kind of time-consuming operation, that time-consuming operation either needs to take time to report its progress by writing a db table or some kind of file on the server OR it needs to be something you can track in some other way -- like querying the database to see how many records of a certain type exist.

            Also, unless the user actually need to wait for the insert, you could simply let them know you have received the data and will process it. Then either close the connection, if you are communicating with a browser, or fork off and perform the time consuming inserts in the background.

              Write a Reply...