Solved

how do I create a pause in the script?

Posted on 2008-10-26
4
757 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
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:ushastry
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 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