Progress bar problem

Hi I would love to display a progress bar when inserting data from one mysql table from another mysql table.

The examples I keep coming across are particularly concerned with progress bars that are displayed when uploading data from a form and displaying it on another form and I just can’t seem make the current code I have to display the bar eventhough the query is executed successfully.

Your help in this regard will be highly appreciated.

Hi kgizzy, welcome to the forums,

I suppose it might be possible to do, but why?

Database INSERT queries happen server-side and usually run so fast that for all intents and purposes they’re “instant”. Even major database manipulalation runs fairly fast.
Any progress bar would likely “blink” and even then passing the info back via an HTTP request would take longer than the INSERT took to run.
IMHO it would be better to send back a simple “success” type of message.

Are you sure you shouldn’t be looking into optimizing your tables and queries instead?

Hi Mittineague

The thing is I should have a progress bar for every operation that occurs on my web based application and the starting point is having a progress bar for a mysql query.

Displaying the progress bar seems to be a mission impossible but everything else works perfectly.

Thanks for taking the time to offer your helping hand its much appreciated.

It’s not impossible, just how do you show the progress for something that typically takes a mere fraction of a second, or only a few seconds at most?

Often the “bottle necks” in a site visitors experience have more to do with the transfer of data via HTTP requests. eg. heavy weight img, swf, files.

Though sometimes slowdown or even hang can be a result of poor code.

Maybe what you are looking for is Benchmarking? Maybe this recent thread will give some ideas? http://www.sitepoint.com/forums/showthread.php?1185776-How-to-measure-time-with-microsecond-precision-on-a-32-bit-system

Any progress bar component will need to be given the update percentage from the process being run. What does the SQL statement look like that is performing the insert? If it is inserting a lot of rows perhaps you could break it in to chunks and report the progress after each chunk?

This would most likely slow the overall insert but would give you the progress if the usability factor was worth it.

Hi Guys

@Mittineague my code already has the duration of the whole execution process which works perfectly its just one aspect of my project that doesn’t seem to work which is the progress bar. My script when executed should display the progress bar and the total time taken to execute the query. At the moment the query does work and the total time taken is functional.

@Jordan Windebank here’s the code snippet:

<?php


     $time_start = microtime(true);

    $mysqlserver = "localhost"; 		
    $user = "root";
    $pass = "";
    $db = "Profusion";

    $link = mysql_connect( "$mysqlserver", $user, $pass );
    if ( ! $link )
    die( "Couldn't connect to MySQL" );
    //print "Successfully connected to server<P>";

    mysql_select_db( $db )
    or die ( "Couldn't open $db: ".mysql_error() );
    //print "Successfully selected database \\"$db\\"<P>";

    $result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from Profusion.source_cdr") or die(mysql_error());
    $progress=mysql_affected_rows();

    $time_end = microtime(true);
    $time = $time_end - $time_start;

    echo  "Total time taken :"." ".round($time,6) . " s";

     ?>

There are 28 rows to be inserted in the table so it’s not really big.

I am trying to utilize the article written by TIm ad he passes the progress bar to a form unfortunately. link below

Yes, but that article is about uploading (the aforementioned HTTP requests) not database profiling.

Is this something you want only for yourself or for every user?

I am running a mini application here at work so its currently for myself. I here what you are saying.

Thanks alot I’ll try and re-direct my approach. Your efforts are highly appreciated.

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.