Re: [PHP] Freing memory resources?

2005-06-16 Thread Richard Lynch
Is they any way you could combine the UPDATE queries?

I suspect not, but those are the ones that are probably what will get
worse and worse over time as the file/db grows.

All the left outer joins are not gonna help either, but that's only a few
hundred rows, right?...  But will it grow?

Finally, if "nice -19" isn't supported on Windows (I suspect not) a simple
"sleep" call (see below) could give the rest of the system a chance to do
useful work.

On Thu, June 16, 2005 3:28 am, Gustav Wiberg said:
>  //This script was updated 2005-06-16
> //by Gustav Wiberg / [EMAIL PROTECTED]
> //Please visit www.varupiraten.se
> //
> require("phpfunctions/dbsafety.php");
>
> //Parameters that sets the rules for importing
> //
> static $fileName = "import/GNTprisfil.txt";
> static $logFileName = "logfiles/gnt.txt";
> static $limitSteps = 25;
> static $saldoColumn = 6;
> static $artNrColumn = 2;
> static $priceColumn = 5;
> static $row = 1;
> static $updateActions = 0;
> static $deleteActions = 0;
> static $checkLev = "gn-";
>
> //Function for creating a logfile
> //and writing to screen
> //
> function writeNow($str, $logFileName) {
>
> //Create a handle for writing (appending)
> //
> $logHandle = fopen($logFileName,"a");
>
>   fwrite($logHandle, "$str\r\n");
>   echo $str . "";
>
> //Close file for writing to logfile
> //
> fclose($logHandle);
>
> }
> //Set limitstart for first time
> //
> if (!isset($limitStart)) {$limitStart = 0;}
>
> if (isset($_REQUEST["updateActions"])) {$updateActions =
> $_REQUEST["updateActions"];}
> if (isset($_REQUEST["deleteActions"])) {$deleteActions =
> $_REQUEST["deleteActions"];}
> if (isset($_REQUEST["startTime"])) {$startTime = $_REQUEST["startTime"];}
>
> if ($_REQUEST["limitstart"]) {
>   $limitStart = $_REQUEST["limitstart"];
> }
>
>
> require ("phpfunctions/opendb.php");
>
>
> $sql = "SELECT COUNT(IDVara) cn FROM tbvara WHERE Varunamn LIKE
> '$checkLev%'";
> $querys = mysql_query($sql);
>
> //Count products in db
> //
> if ($limitStart == 0) {
>
>   $dbArray = mysql_fetch_array($querys);
>   $nrOfProducts = $dbArray["cn"];
>   echo "Antal produkter: $nrOfProducts";
>
> //Create logfile or delete all content from current logfile
> //
> $logHandle = fopen($logFileName,"wb");
> fclose($logHandle);
>
>
>   //Get starttime of script
>   //
>   $startTime = time();
> }
>
> else {
>
>   $nrOfProducts = $_REQUEST["nrofproducts"];
>
>}
>
>if ($limitStart > $nrOfProducts) {
>  $nrSeconds = time() - $startTime;
>  $nrMinutes = $nrSeconds / 60;
>  ?>
>  Klar med uppdatering av saldo och ev. borttagningar för
> GNT.
>  Tid för uppdatering:  sekunder eller
>  minuter
>  Antal uppdateringar: 
>  Antal produkter som ej visas efter uppdatering:   $deleteActions;?>
>   href="captech_checksaldo_step2.php?deleteActions=0&updateActions=0">Kolla
> captech produkter
>exit;
>}
>
> //Check if the filename exists first!
> //If not, then exit script
>
> if (!file_exists($fileName)) {
>
> echo "Filen $fileName finns inte!Avslutar scriptet nu!";
> exit;
>
> }
>
>
>
>
> //Go through database with products from GNT
> //
> $sql = "SELECT tbvara.IDVara, tbvara.Saldo, tbvara.startPris,
> tbvara.Varunamn, tbvara.synligVara, tbvara.lastPris,
> tbunderkategori.marginalProcent, tbunderkategori.Underkategori FROM
> tbvara";
> $sql .= " LEFT JOIN tbunderkategorivara ON (tbvara.IDVara =
> tbunderkategorivara.ForIDVara)";
> $sql .= " LEFT JOIN tbunderkategori ON
> (tbunderkategori.IDUnderKategori
> = tbunderkategorivara.ForIDUnderKategori)";
> $sql .= " WHERE Varunamn LIKE '$checkLev%' AND
> tbunderkategori.marginalProcent>-1 AND tbunderkategori.Underkategori<>'Ej
> tilldelade' ORDER BY IDVara LIMIT $limitStart,$limitSteps";
> //echo $sql;
> //exit;
> $querys = mysql_query($sql);
>
> while ($toarray = mysql_fetch_array($querys)) {
>
> //Get current row from db
> //
> $idproduct = $toarray["IDVara"];
> $dbSaldo = $toarray["Saldo"];
> $dbArtNr = $toarray["Varunamn"];
> $dbPris = $toarray["startPris"];
> $dbSynligVara = $toarray["synligVara"];
> $dbLastPris = $toarray["lastPris"];
> $dbMarginalProcent = $toarray["marginalProcent"];
> $dbUnderKategori = $toarray["Underkategori"];
>
> //Taken from table tbunderkategori
> //
> $marginalProcent = intval($dbMarginalProcent);
>
> //Delete $checkLev from string $dbArtNr for the sake of
> comparing
> //(take away the three first characters)
> //
> $dbArtNr = substr($dbArtNr, 3);
>
>
> //Go through whole textfile and compare with current row in db
> //
> $handle = fopen($fileName, "r");
>
>
>
> $foundProduct= false;
> ob_start();
>
> while (($data = fgetcsv($handle, 1

Re: [PHP] Freing memory resources?

2005-06-16 Thread Jochem Maas

Gustav Wiberg wrote:

Hi there!

I have a script that runs about 10 minutes... Localy on my computer 
(Windows XP 2.6Ghz, Apache) the computer hangs a little now and then 
when the script is running. Is there any good way of releasing more 
memory while the script is running? The script could take 20 minutes, 
it's not a big matter how long the script takes. (As long as the server 
can do other things at the same time)


(In production environment, the php-script is on Apache and Linux (if it 
matters))


maybe use nice to stop your script hogging resources.

$> nice --adjustment=19 php yourscript.php

check the manpage for more info:

$> man nice



Best regards
Gustav Wiberg
@varupiraten.se



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Freing memory resources?

2005-06-16 Thread Gustav Wiberg

Hi there!

I have a script that runs about 10 minutes... Localy on my computer (Windows 
XP 2.6Ghz, Apache) the computer hangs a little now and then when the script 
is running. Is there any good way of releasing more memory while the script 
is running? The script could take 20 minutes, it's not a big matter how long 
the script takes. (As long as the server can do other things at the same 
time)


(In production environment, the php-script is on Apache and Linux (if it 
matters))


Best regards
Gustav Wiberg
@varupiraten.se

Here is my script:
";

//Close file for writing to logfile
//
fclose($logHandle);

}
//Set limitstart for first time
//
if (!isset($limitStart)) {$limitStart = 0;}

if (isset($_REQUEST["updateActions"])) {$updateActions = 
$_REQUEST["updateActions"];}
if (isset($_REQUEST["deleteActions"])) {$deleteActions = 
$_REQUEST["deleteActions"];}

if (isset($_REQUEST["startTime"])) {$startTime = $_REQUEST["startTime"];}

if ($_REQUEST["limitstart"]) {
 $limitStart = $_REQUEST["limitstart"];
}


require ("phpfunctions/opendb.php");


   $sql = "SELECT COUNT(IDVara) cn FROM tbvara WHERE Varunamn LIKE 
'$checkLev%'";

   $querys = mysql_query($sql);

   //Count products in db
   //
   if ($limitStart == 0) {

 $dbArray = mysql_fetch_array($querys);
 $nrOfProducts = $dbArray["cn"];
 echo "Antal produkter: $nrOfProducts";

   //Create logfile or delete all content from current logfile
   //
   $logHandle = fopen($logFileName,"wb");
   fclose($logHandle);


 //Get starttime of script
 //
 $startTime = time();
   }

   else {

 $nrOfProducts = $_REQUEST["nrofproducts"];

  }

  if ($limitStart > $nrOfProducts) {
$nrSeconds = time() - $startTime;
$nrMinutes = $nrSeconds / 60;
?>
Klar med uppdatering av saldo och ev. borttagningar för 
GNT.
Tid för uppdatering:  sekunder eller 
 minuter

Antal uppdateringar: 
Antal produkter som ej visas efter uppdatering:  $deleteActions;?>
href="captech_checksaldo_step2.php?deleteActions=0&updateActions=0">Kolla 
captech produkter

Avslutar scriptet nu!";
   exit;

   }




   //Go through database with products from GNT
   //
   $sql = "SELECT tbvara.IDVara, tbvara.Saldo, tbvara.startPris, 
tbvara.Varunamn, tbvara.synligVara, tbvara.lastPris, 
tbunderkategori.marginalProcent, tbunderkategori.Underkategori FROM tbvara";
   $sql .= " LEFT JOIN tbunderkategorivara ON (tbvara.IDVara = 
tbunderkategorivara.ForIDVara)";
   $sql .= " LEFT JOIN tbunderkategori ON (tbunderkategori.IDUnderKategori 
= tbunderkategorivara.ForIDUnderKategori)";
   $sql .= " WHERE Varunamn LIKE '$checkLev%' AND 
tbunderkategori.marginalProcent>-1 AND tbunderkategori.Underkategori<>'Ej 
tilldelade' ORDER BY IDVara LIMIT $limitStart,$limitSteps";

   //echo $sql;
   //exit;
   $querys = mysql_query($sql);

   while ($toarray = mysql_fetch_array($querys)) {

   //Get current row from db
   //
   $idproduct = $toarray["IDVara"];
   $dbSaldo = $toarray["Saldo"];
   $dbArtNr = $toarray["Varunamn"];
   $dbPris = $toarray["startPris"];
   $dbSynligVara = $toarray["synligVara"];
   $dbLastPris = $toarray["lastPris"];
   $dbMarginalProcent = $toarray["marginalProcent"];
   $dbUnderKategori = $toarray["Underkategori"];

   //Taken from table tbunderkategori
   //
   $marginalProcent = intval($dbMarginalProcent);

   //Delete $checkLev from string $dbArtNr for the sake of 
comparing

   //(take away the three first characters)
   //
   $dbArtNr = substr($dbArtNr, 3);


   //Go through whole textfile and compare with current row in db
   //
   $handle = fopen($fileName, "r");



   $foundProduct= false;
   ob_start();

   while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

  $textSaldo = $data[$saldoColumn];
  $textArtNr = $data[$artNrColumn];
  $textPris = $data[$priceColumn];


   //Default marginal is 15 when marginal isn't set for 
undercategory

   //
   if (intval($marginalProcent) == 0) {

 $marginalProcent = 15;

   }

   //echo "Marginal för underkategori $dbUnderKategori är 
$marginalProcent%";


   //Set price based on price from textfile + marginal set
   //for undercategory
   //
   $newPrice = $textPris + (($textPris * 
($marginalProcent/100)));

   $newPrice = round($newPrice,0);


  if (isset($textArtNr) AND isset($textSaldo) AND $row>1) {


   //If there is an occurence of article-nr for product in 
textfile

   //that matches the one in current row in database...
   //
   if ($textArtNr == $dbArtNr) {

 $foundProduct= true;

   //If saldo for product isn't the same for product in 
textfile,