Solved

how do I create a pause in the script?

Posted on 2008-10-26
4
764 Views
Last Modified: 2013-12-13
I have written a script in php that uses php and mysql to check a column of thumbnails in my database.  The only issue is that since I am checking over 150,000 image URL's, the script keeps timing out.  I am notr using MYPHP ADMIN to execute the script, it is being strictly in PHP and MYSQL.  I am attaching the snippit of code so that yo ucan see and understand the loop to tel lme how to break it up.  I want the script to do it about 75 rows at a time.  To do 75 lines, close the database, reload the page, reopen the database, do another 75, close the database, reload the page, reopen the database, and so on......  Can you please show me how to change my script to accomplish this to avoid time-outs and to make the scipt run smoother?  Below is the script with sensitive data covered with **** and a brief description of each part.

<?php
$link = mysql_connect('localhost', '*****', '*****');
if (!$link) {die('Could not connect: ' . mysql_error());}
$db_selected = mysql_select_db('******');
if (!$db_selected) {
    die('Could not select database: ' . mysql_error());}
echo ("Connected successfully<BR>");
 
***Everything above just connects us to the database***
$query = ("SELECT *********** from ****************");
$result = mysql_query($query);
***Everything above gets THE DATA***
 
while($row = mysql_fetch_assoc($result))
      {$*************2 = $row['************'];
      $manu = substr($productcode2,0,3); 
	$part = substr($productcode2,3,30);
	$url= "http://www.*********.com/images/00/partimages/" . $manu . "/" . $part . ".jpg";              
	$imagetype = "image/jpeg";
***ABOVE GETS THE DATA INTO THE NEEDED VARIABLES WHILE IN THE LOOP***
 
       
            if($handle = fopen($url, "rb")){$content = fread($handle, 4);}
	      if(!$content ){$url = "http://www.*********.com/images/00/VendorPics/" . $manu . ".gif";}
		if($handle = fopen($url, "rb")){$content = fread($handle, 4);}
	      if(!$content ){$url = "http://www.***************.com/skin1/images/large_company_logo.gif";}
		
		
		$endchar = substr($url,-1);
		if($endchar == "f"){$imagetype = "image/gif";}		*****ABOVE  CREATES THE DATA NEEDED TO CREATE THE SQL COMMANDS.....STILL WITHIN THE LOOP****		
		
		$newquery1 = ("update `xcart_thumbnails` set `image_path` = '$url' where `productcode` = '$productcode2'");
		$newquery2 = ("update `xcart_thumbnails` set `image_type` = '$imagetype' where `productcode` = '$productcode2'");
***ABOVE CREATES THE ACTUAL SQL COMMANDS.....STILL WITHIN LOOP****
 
		echo"productcode2<BR>";
		mysql_query($newquery1);
		mysql_query($newquery2);
***EXECUTES ACTUAL SQL COMMANDS.....STILL WITHIN LOOP****
		
		$content=(!$content);}
***RESETS A VARIABLE ATTHE END OF THE LOOP AND ENDS LOOP******
 
 
mysql_close($link);
****CLOSES THE LINK TO THE DATABASE*****
?>

Open in new window

0
Comment
Question by:THE_HORSEPOWERSHOP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 7

Expert Comment

by:ljubiccica
ID: 22806649
Hey there!

Tried to use sleep() function?

http://www.php.net/sleep

KR,
ljubiccica
0
 
LVL 26

Expert Comment

by:Umesh
ID: 22807050
0
 

Author Comment

by:THE_HORSEPOWERSHOP
ID: 22810421
I can't figure out how to get the pagination to work.  Is there any way I can get the script to go through line by line, rather than by doing a query, getting all 200,000 lines out of the database, and then trying to do it line by line.  My mysql server is not liking it at all!  LOL.  I would like to do it line by line.  Any suggestions?
0
 
LVL 5

Accepted Solution

by:
ionutz2k2 earned 500 total points
ID: 22829762
You can try to use 'limit' in the sql query to select N rows. Store the indexes used (how many of the pictures parsed) in a SESSION variable reload the page and read the indexes nedded from the SESSION variable so you can continue from where you stoped before reloding.
The algorithm would go something like this:
- read index from SESSION (index is not set in session so initial index is 0)
- select and parse N images startying from row 0
- store N in the SESSION variable
- reload the page
---------------------
- read index from SESSION (index is N)
- select and parse N images starting from row N
- store 2*N in the SESSION variable
- reload the page

and so on...
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question