Jump to content

[SOLVED] Script never indicates it ends, loading 11,000 line csv


HawkCode

Recommended Posts

???

I have a script, PHP 4, that loads an 11,000 line csv file into a MySql data base. Running it loacally It completes and gives me the stats that I echo at the end.

 

Running on the server it does put in all the records, but never gives me the stats, there fore giving the illusion that it is in an endless loop.

 

If I cut the file down it ends as expected?

 

Any ideas? 

 

Thanks

 

Link to comment
Share on other sites

Ok here's the code. It actuall takes less than 5 minutes to run on server.

 

When I cut down the source file I cut it down to about 25% of the original.

 

The data goes go in ok, when I ran it on my machine I did it in Zend.

 

function import_access () {
set_time_limit(3600);
global $Main;
$path_to_csv = "WebSiteAccess.csv";
$csv_delimiter = ","; 


$NewAddedcount = 0;
$Skipcount = 0;
$PrimaryChangeCount = 0;
$SecondaryChangeCount = 0;
$AccessLevelCount = 0;
$CheckSum = 0;
$LastPID = 0;
$PIDCount = 0;
$PreviousNew = 0;
echo "About to open:".$path_to_csv . "\n";
$handle = fopen ($path_to_csv,"r");
$Cnt = 0;
$CharsToRemove = array(" ", "-", "'", ".");
$LastLogin = "";
$LastLoginCount = 0;
while (($data = fgetcsv ($handle, 1000, $csv_delimiter)) !== FALSE) {

	$CheckSum = $NewAddedcount + $Skipcount + 
							$PrimaryChangeCount + $SecondaryChangeCount + $AccessLevelCount;

	$PeopleID = mysql_escape_string($data[0]); 				//tblPeople.[Person ID]

	//$login = ereg_replace("[^A-Za-z0-9]", "", $login);
	$ChapterCode = mysql_escape_string($data[1]);													//tblPeople.ChapterCode,
	$MembershipNumber = mysql_escape_string($data[2]);			//tblPeople.MemberNumber, 
	$Address1 = mysql_escape_string($data[3]);			//tblPeopleAddresses.Address1
	// For xCart PW use addslashes(text_crypt($data[4]));
	$Address2 = mysql_escape_string($data[4]);				//tblPeopleAddresses.Address2
	$City = mysql_escape_string($data[5]);						//tblPeopleAddresses.City
	$State = mysql_escape_string($data[6]);						//tblPeopleAddresses.State
	$Zip = mysql_escape_string($data[7]);							//tblPeopleAddresses.Zip
	$Country = mysql_escape_string($data[8]);
	$Title = mysql_escape_string($data[9]);						//tblPeople.Title
	$FName = mysql_escape_string($data[10]);					//tblPeople.[First Name]
	$LName = mysql_escape_string($data[11]);					//tblPeople.[Last Name]
	$NameSuffix = mysql_escape_string($data[12]);			//tblPeople.[Name Suffix]
	$EMail = mysql_escape_string($data[13]);					//tblPeople.Email
	$Phone = mysql_escape_string($data[14]);					//tblPeopleAddresses.Phone
	$Fax = mysql_escape_string($data[15]);						//tblPeopleAddresses.Fax
	$AddressType = mysql_escape_string($data[16]);		//tblAddressTypes.[Address Type Desc]
	$PrimaryAddr = mysql_escape_string($data[17]);		//tblPeopleAddresses.Primary
	$CardSent = mysql_escape_string($data[18]);				//tblPeople.CardsSent
	$MemStatus = mysql_escape_string($data[19]);			//tblMembership.Status
	$AccessLevel = mysql_escape_string($data[20]);
	if ($AccessLevel == null) {
		$AccessLevel = 0;
	}



	$OldLogin = $LName . $MembershipNumber;
	$OldLogin = StrToLower(ereg_replace("[^A-Za-z0-9]", "", $OldLogin));


//		if ($PeopleID == 31702) {
//			Echo "Stop\n";
//		}
	if ($PeopleID == $LastPID) {
		$PIDCount++;
	}else {
		$PIDCount = 1;
		$PreviousNew = 0;
	}


	if ($MemStatus == 1){
		$Password = $ChapterCode . $MembershipNumber;
	} else if ($PrimaryAddr ==1){
		$Password = strtolower($LName) . $Zip;
	}
	$Password = crypt($Password,"aspe");
	$sql = "SELECT * FROM accesslevel WHERE PeopleID = " . $PeopleID;
	$result = mysql_query($sql, $Main )or die(mysql_error());
	$row_result = mysql_fetch_assoc($result);
	$num_rows = mysql_num_rows($result);
	if ($num_rows == 0):{			// New person Add to accesslevel db
		$NewAddedcount++;
		$PreviousNew = 1;

		$InsertSQL = "INSERT INTO accesslevel(PeopleID, EMail, Password, AccessLevel,ASPEMemberID, 
									FName, LName, Title, NameSuffix, Phone, FirstLogin, OldLogin )
			VALUES(" . $PeopleID . ", '$EMail', '$Password', '$AccessLevel', '$MembershipNumber', '$FName', '$LName',
				'$Title', '$NameSuffix', '$Phone', 1, '$OldLogin')";
		mysql_query($InsertSQL)
				or die("<br>Error doing insert:".mysql_error());

		if ($PrimaryAddr == 1): {
			$updateSQL = sprintf("UPDATE accesslevel SET PAddress1='%s', PAddress2='%s',
											 PCity='%s', PState='%s', PZip='%s', PCountry='%s' 
											 WHERE PeopleID=%s",
												$Address1, $Address2, $City, $State, $Zip, $Country, $PeopleID);

		} 
		else: { 
			$updateSQL = sprintf("UPDATE accesslevel SET SAddress1='%s', SAddress2='%s',
											 SCity='%s', SState='%s', SZip='%s', SCountry='%s'
											 WHERE PeopleID=%s",
												$Address1, $Address2, $City, $State, $Zip, $Country, $PeopleID);

		}
		endif;
		$Result1 = mysql_query($updateSQL) or die(mysql_error());
	}	
	else: {

		//1st Check if Address Changed, if so update inc AccessLevel

		if (($PrimaryAddr == 1) and ($row_result['PAddress2'] !=  stripslashes($Address2)) and ($PIDCount < 3)): {
			$updateSQL = sprintf("UPDATE accesslevel SET PAddress1='%s', PAddress2='%s',
											 PCity='%s', PState='%s', PZip='%s', PCountry='%s', AccessLevel=%s 
											 WHERE PeopleID=%s",
												$Address1, $Address2, $City, $State, $Zip, $Country, $AccessLevel, $PeopleID);

			if ($PreviousNew == 0) {
				$PrimaryChangeCount++; 	
			 } 
			$Result1 = mysql_query($updateSQL) or die(mysql_error());
		} 
		elseif (  ($row_result['SAddress2'] != stripslashes($Address2)) and 
							($PrimaryAddr == 0)	 and ($PIDCount < 3)):  { 
			$updateSQL = sprintf("UPDATE accesslevel SET SAddress1='%s', SAddress2='%s',
											 SCity='%s', SState='%s', SZip='%s', SCountry='%s', AccessLevel=%s
											 WHERE PeopleID=%s",
												$Address1, $Address2, $City, $State, $Zip, $Country, $AccessLevel, $PeopleID);
			if ($PreviousNew == 0) {
				$SecondaryChangeCount++;													
			}
			$Result1 = mysql_query($updateSQL) or die(mysql_error());
		} //All info same, Now check accesslevel
		elseif (($AccessLevel != $row_result['AccessLevel'])  and ($PIDCount < 3)): {   
			$updateSQL = sprintf("UPDATE accesslevel SET  AccessLevel=%s
											 WHERE PeopleID=%s",$AccessLevel, $PeopleID);
			if ($PreviousNew == 0) {
				$AccessLevelCount++;
			}
			$Result1 = mysql_query($updateSQL) or die(mysql_error());


		}
		endif;

	}	
	endif;
	$LastPID = $PeopleID;
	if (		$CheckSum == ($NewAddedcount + $Skipcount + 
							$PrimaryChangeCount + $SecondaryChangeCount + 
							$AccessLevelCount)) {
		if ($PreviousNew == 0) {
			$Skipcount++;
		}
	}
} 


echo "Total New Added: " . $NewAddedcount ."<br>";
echo "        Skipped: " . $Skipcount ."<br>";
echo " Prim Addr Chng: " . $PrimaryChangeCount ."<br>";
echo " Sec Addre Chng: " . $SecondaryChangeCount ."<br>";
echo " AccessLvl Chng: " . $AccessLevelCount ."<br>";
echo "<h3>finished Inserted: $count</h3>"; //this is more usefull than it looks
fclose($handle);
};

Link to comment
Share on other sites

It could be a memory limit issue in PHP. It can only allocate a certain amount of memory.

 

Also a trick I learned is that sometimes the browser cuts out on you, so like every 100 or 200 records do something like this:

 

<?php

for ($i=1;$i<2000;$i++) {
    if ($i%200) {
        echo '.';
        sleep(1); // echo the dot and sleep the script for 1 second
    }
}
?>

 

That will send output to the browser making it not timeout and think it is still alive.

Link to comment
Share on other sites

More: I cut the 11,000 line csv in half and it took 1:39 to run AND it displayed the stats at the end.

 

 

Maybe for this initial load I have to do it in pieces? Seems silly.

 

 

Rich

 

Shouldn't have to, just have to take the time to figure out whats holding it up. The question is, is it worth your time to figure out for this one time, or will this be a recurring event that it would be worth your time?

 

If this is only a "one time" deal than I would just go with cutting it in half and be done. If it will reoccur than take the time now to figure it out.

Link to comment
Share on other sites

I think it's definatly worth it, just to know what the cause is for the future!

 

I Added:

	$TotalCOunt++;
	if ($TotalCOunt % 100) {
		echo '.';
      sleep(1); 
	}

 

It is not outputting anything to server, this just slows it way down...

 

 

 

 

Link to comment
Share on other sites

 

No Not solved. When the script ends it prints out stats, when I run it locally in Zend Debug it works fine. If I cut the csv down to half the size, 5,000 lines it works.

 

When run with the full csv 11,000 lines, the script never outputs the stats, BUT IT DOES COMPLETE, because I look at the total number of records in the MySql with Navicat and it has finished inserting the proper number of records.

 

So no it is definatly not solved.

 

Rich

 

Link to comment
Share on other sites

So it seems the browser essentially stops itself at a certain point, probably around 3 minutes.

 

What might work is to try storing the stats in a session variable and than redirect to a page, once completed that displays the stats, or maybe try to write them to a file?

 

Another option, and I am not sure if this will work, but you could create the function to check the size of the file, if the file is bigger than x amount of bytes you split the file in half and run the first part, display the stats and than run the second part, that may keep the browser alive and kicking.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.