Results 1 to 5 of 5

Thread: need a php function that does this

  1. #1
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default need a php function that does this

    Hello you all. I am trying to find or code( never written a php function before, manage to get by without it) a function that does the following:

    run a process by means of a loop based on how many records in a database. Now the process may run for more that 30 seconds due to the size of the database, how do i get the function to check the time and continue with the process after 30 seconds until the database reocrds are processed and dont have the page time out.

    for example this simple script does what i want but it only works based on the server time of 30 seconds:

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    if(isset($_SESSION['randomUserId']))
    {
    $rsstalecartitems = "SELECT ter_orderlist_ord.id_ord
    FROM ter_proddet_prd where ter_history_his.ip_his = '12345' ";


    $rsstalecartitems_result = mysql_query($rsstalecartitems,$ksConnection) or die(mysql_error());

    $num_rows = mysql_num_rows($rsstalecartitems_result);
    //echo $num_rows;

    if($num_rows != ""){
    $i=0;
    while ($row_rsstalecartitems=mysql_fetch_assoc($rsstalec artitems_result)){

    $addedby = $row_rsstalecartitems['idusr_ord'];

    $i++;

    $deletecartinfo = "delete from ter_orderlist_ord where idusr_ord='$addedby'";
    $deletecartinfo_result = mysql_query($deletecartinfo,$ksConnection) or die(mysql_error());

    $deleteuserinfo = "delete from ter_user_usr where id_usr='$addedby'";
    $deleteuserinfo_result = mysql_query($deleteuserinfo,$ksConnection) or die(mysql_error());


    }

    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Any Help is appreciated.
    Last edited by leocrawf; Dec 5, 2008 at 02:57 PM.
    "...men are that they might have joy."
    Nephi

  2. #2
    Join Date
    Aug 2004
    Posts
    6
    Rep Power
    0

    Default

    It maybe easier to modify the timeout of the script by using set_time_limit(300);

    This will set the time out to 5mins i.e 300 seconds.

    details here - www.php.net/set_time_limit

    If you still want to extend the time by 30 seconds you do some thing in the loop like


    $startTime = time();

    $i=0;
    while ($row_rsstalecartitems=mysql_fetch_assoc($rsstalec artitems_result)){

    $diff = time() - $startTime ;

    if ($diff >= 15) ;// You should use a number that is less than the maximum execution time
    {

    set_time_limit(30); //extend the time

    $startTime = time(); //reset start time
    }


    .
    .
    .
    rest of code follows

  3. #3
    Join Date
    Feb 2006
    Posts
    4,242
    Rep Power
    0

    Default

    Did you get the solution to work?
    |--- www.RealJamaicaEstate.com ™ ---|
    Invest small = small returns [micro enterprise] | Invest Big = returns Big [macro enterprise]
    --- www.fashionsJAMAICA.com ™ -|- www.ChampsJamaica.com

  4. #4
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    1. keep track of the id of the last record processed in your while loop (in the session or something).
    2. keep track of the time elapsed in the while loop
    3. whenever the page is close to timeout, end the loop so the request completes
    4. allow the user to see how many of the records were processed and continue from the last one (or use a redirect so it is automatic )
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  5. #5
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    I will give the solution a try. Hope it works
    .
    "...men are that they might have joy."
    Nephi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •