jakebur01 Posted February 25, 2009 Share Posted February 25, 2009 How can I make my script work without getting this: CGI Timeout The specified CGI application exceeded the allowed time for processing. The server has deleted the process. I tried adding a little for loop and limiting it to take a break every 250 rows but it still does not help. mysql_query("UPDATE {$tablename} AS a, {$tablename} AS b SET a.$list =b.$list, a.$dealer=b.$dealer, a.$distributor=b.$distributor, a.$sort=b.$sort, a.$stdpack=b.$stdpack WHERE LEFT(b.$part, CHAR_LENGTH(b.$part)-2)=a.$part AND RIGHT(b.$part, 2) IN ('-1', '-2', '-3', '-0')"); mysql_query("DELETE FROM {$tablename} WHERE $dealer IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); mysql_query("DELETE FROM {$tablename} WHERE $distributor IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); mysql_query("DELETE FROM {$tablename} WHERE $list IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); echo"Removing rows where the retail price is zero.<br /><br />Writing File... Please wait.."; $source_file = "C:/Inetpub/Websites/mysite.com/price/$filename"; $fp= fopen("$source_file", "w"); // get count of how many rows we'll be dealing with $total_rows = mysql_num_rows(mysql_query("SELECT * FROM {$tablename} AND $sort > '699'")); // how many rows per loop execution - you can up this too, probably like a 1000 is a good number $rows_per_loop = 250; // Get the most amount of loops possible. $total_loops = ceil($total_rows/$rows_per_loop); // just make it look nice: echo 'Running rows: <br />'; // run the loop, while loop counter is less than the total number of loops: for($i=0; $i<$total_loops; $i++) { // get the numbers for the limit, // start is just the current loop number multiplied by the rows per loop // and end is loop counter + 1, multiplied by the rows per loop $limit_start = $rows_per_loop*$i; $limit_end = $rows_per_loop*($i+1); $result = mysql_query("SELECT * FROM {$tablename} AND $sort > '699'"); $i=0; while ($row = mysql_fetch_assoc($result)) { if (($i%100) == 0) { echo "."; ob_flush(); flush(); } $i++; $partnumber=$row["$part"]; $targetChars=array('"', '$', ',', '*'); $stdpack=$row["$stdpack"]; $stdpack=str_replace($targetChars, "", $stdpack); $dealercheck=$row["$dealer"]; $dealercheck=str_replace($targetChars, "", $dealercheck); $dealercheck = number_format($dealercheck, 2, '.', ''); $distributorcheck=$row["$distributor"]; $distributorcheck=str_replace($targetChars, "", $distributorcheck); $distributorcheck = number_format($distributorcheck, 2, '.', ''); $listprice=$row["$list"]; $listprice=str_replace($targetChars, "", $listprice); $listprice = number_format($listprice, 2, '.', ''); $distributor=$row["$distributor"]; $distributor=str_replace($targetChars, "", $distributor); $cost=$distributor*.95; $cost = number_format($cost, 2, '.', ''); $level1=$row["$dealer"]; $level1=str_replace($targetChars, "", $level1); $level1 = number_format($level1, 2, '.', ''); $level2=$row["$dealer"]; $level2=str_replace($targetChars, "", $level2); $level2=$level2*.96; $level2 = number_format($level2, 2, '.', ''); $level3=$row["$dealer"]; $level3=str_replace($targetChars, "", $level3); $level3=$level3*.92; $level3 = number_format($level3, 2, '.', ''); $level4=$row["$dealer"]; $level4=str_replace($targetChars, "", $level4); $level4=$level4*.9; $level4 = number_format($level4, 2, '.', ''); $level5=$row["$dealer"]; $level5=str_replace($targetChars, "", $level5); $level5=$level5*.88; $level5 = number_format($level5, 2, '.', ''); $level6=$row["$dealer"]; $level6=str_replace($targetChars, "", $level6); $level6=$level6*.85; $level6 = number_format($level6, 2, '.', ''); $c1="1075"; $c2="$partnumber"; $c3=""; $c4=""; $c5="$stdpack"; $c6=""; $c7=""; $c8="$listprice"; $c9="$listprice"; $c10="$cost"; $c11="$level1"; $c12="$level2"; $c13="$level3"; $c14="$level4"; $c15="$level5"; $c16="$level6"; $c17="0"; $c18="0"; $c19="0"; $c20="0"; $c21="0"; $c22="0"; $c23="0"; $c24="0"; $c25="0"; $c26=""; if($distributorcheck==$dealercheck) { $c11="$dealercheck"; $c12="$dealercheck"; $c13="$dealercheck"; $c14="$dealercheck"; $c15="$dealercheck"; $c16="$dealercheck"; } else { $result2 = mysql_query("SELECT * FROM smith_prog_item WHERE `item` = $partnumber LIMIT 1"); while ($row2 = mysql_fetch_assoc($result2)) { if($row2["type"]=="NGK") { $c17="100"; $c18="$level6";} elseif ($row2["type"]=="OTHER") { $c11="$level6"; $c12="$level6"; $c13="$level6"; $c14="$level6"; $c15="$level6"; $c16="$level6"; } } } $outputstring = $c1. "\t".$c2."\t".$c3."\t".$c4."\t".$c5."\t".$c6."\t".$c7."\t".$c8."\t".$c9."\t".$c10."\t".$c11."\t".$c12."\t".$c13."\t".$c14."\t".$c15."\t".$c16."\t".$c17."\t".$c18."\t".$c19."\t".$c20."\t".$c21."\t".$c22."\t".$c23."\t".$c24."\t".$c25."\t".$c26."\t".$c27."\t".$c28."\t".$c29."\n"; //print $outputstring;echo"<br />"; fwrite($fp, $outputstring, strlen($outputstring)); } mysql_free_result($result); // show where we are at echo $limit_start,' - ',$limit_end,'<br />'; sleep(1); // give PHP a little nap to keep from overloading server ob_flush(); // as recommended by MadTechie, in case we go beyond the max execution time flush(); // add flush, to make sure it is outputted } //end for loop fclose($fp); echo"<br /><br /><a href=\"http://mysite.com/price/$filename\">Click here to preview file</a>"; Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 bump. . . Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 Is it ok to have this much coding on one page? Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2009 Share Posted February 26, 2009 Your code isn't particularly easy to read because of a lack of consistent indentation and poor variable names so I haven't really bothered to look through it. The amount of code doesn't usually matter but if your executing queries within loops, or even lots of loops within loops you can bog down the server. Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 Premiso made a comment one time about my indentations. How can I learn to become better at indenting. I want to learn to be a better coder. Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2009 Share Posted February 26, 2009 Its all about consistency, as long as you keep your indentation consistent it should be easy to following. Basically each block of code should have its contents indented. eg; if ($expression) { // indent and do something echo $foo; } // <-- end the block at the same indentation point you started at. A more complex example executing a common database query. $sql = "SELECT foo FROM bar"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_object($result)) { echo "{$row->foo</br>}"; } } else { echo "No result found"; // <-- this is at the same depth as the while above because this code will execute if mysql_num_rows() returns false while the while will execute if it (mysql_num_rows()) returns true. } } Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 Thank you. I will practice more indenting. I tried commenting out the multiple queries and loops within loops. But, the progress bar in my browser gets stuck and the server is still timing out. I am posting columns to this page that I selected on the previous page. I am building a text file using the columns that were selected on the previous page. I also tried using LIMIT 1 on my query to see if I could get it to process just one row. But, that did not help. <?php $filename=$_POST['filename']; $path=$_POST['path']; $tablename=$_POST['tablename']; $header=$_POST['header']; $list=$_POST['list']; $dealer=$_POST['dealer']; $distributor=$_POST['distributor']; $sort=$_POST['sort']; $part=$_POST['part']; $stdpack=$_POST['stdpack']; ini_set('max_execution_time', '999'); $db = mysql_connect('localhost', 'user', 'password') or die(mysql_error()); mysql_select_db('database') or die(mysql_error()); if($header==YES) { mysql_query("DELETE FROM {$tablename} LIMIT 1"); echo"Removing header....<br /><br />"; } echo "filename: $filename<br />"; echo "path: $path<br />"; echo "tablename: $tablename<br />"; echo "header: $header<br />"; echo "list: $list<br />"; echo "dealer: $dealer<br />"; echo "distributor: $distributor<br />"; echo "sort: $sort<br />"; echo "part: $part<br />"; echo "stdpack: $stdpack<br />"; /* mysql_query("UPDATE {$tablename} AS a, {$tablename} AS b SET a.$list =b.$list, a.$dealer=b.$dealer, a.$distributor=b.$distributor, a.$sort=b.$sort, a.$stdpack=b.$stdpack WHERE LEFT(b.$part, CHAR_LENGTH(b.$part)-2)=a.$part AND RIGHT(b.$part, 2) IN ('-1', '-2', '-3', '-0')"); mysql_query("DELETE FROM {$tablename} WHERE $dealer IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); mysql_query("DELETE FROM {$tablename} WHERE $distributor IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); mysql_query("DELETE FROM {$tablename} WHERE $list IN('', '$-', '0', '0.0', '0.00', '$ 0', '$ -', '$0.0', '$0.00', '-', ' ', '$')"); echo"Removing rows where the retail price is zero.<br /><br />Writing File... Please wait.."; */ $source_file = "C:/Inetpub/Websites/mysite.com/price/$filename"; $fp= fopen("$source_file", "w"); $result = mysql_query("SELECT * FROM {$tablename} WHERE $sort > '699' LIMIT 1"); while ($row = mysql_fetch_assoc($result)) { $partnumber=$row["$part"]; $targetChars=array('"', '$', ',', '*'); $stdpack=$row["$stdpack"]; $stdpack=str_replace($targetChars, "", $stdpack); $dealercheck=$row["$dealer"]; $dealercheck=str_replace($targetChars, "", $dealercheck); $dealercheck = number_format($dealercheck, 2, '.', ''); $distributorcheck=$row["$distributor"]; $distributorcheck=str_replace($targetChars, "", $distributorcheck); $distributorcheck = number_format($distributorcheck, 2, '.', ''); $listprice=$row["$list"]; $listprice=str_replace($targetChars, "", $listprice); $listprice = number_format($listprice, 2, '.', ''); $distributor=$row["$distributor"]; $distributor=str_replace($targetChars, "", $distributor); $cost=$distributor*.95; $cost = number_format($cost, 2, '.', ''); $level1=$row["$dealer"]; $level1=str_replace($targetChars, "", $level1); $level1 = number_format($level1, 2, '.', ''); $level2=$row["$dealer"]; $level2=str_replace($targetChars, "", $level2); $level2=$level2*.96; $level2 = number_format($level2, 2, '.', ''); $level3=$row["$dealer"]; $level3=str_replace($targetChars, "", $level3); $level3=$level3*.92; $level3 = number_format($level3, 2, '.', ''); $level4=$row["$dealer"]; $level4=str_replace($targetChars, "", $level4); $level4=$level4*.9; $level4 = number_format($level4, 2, '.', ''); $level5=$row["$dealer"]; $level5=str_replace($targetChars, "", $level5); $level5=$level5*.88; $level5 = number_format($level5, 2, '.', ''); $level6=$row["$dealer"]; $level6=str_replace($targetChars, "", $level6); $level6=$level6*.85; $level6 = number_format($level6, 2, '.', ''); $c1="1075"; $c2="$partnumber"; $c3=""; $c4=""; $c5="$stdpack"; $c6=""; $c7=""; $c8="$listprice"; $c9="$listprice"; $c10="$cost"; $c11="$level1"; $c12="$level2";$c13="$level3"; $c14="$level4"; $c15="$level5"; $c16="$level6"; $c17="0"; $c18="0"; $c19="0"; $c20="0"; $c21="0"; $c22="0"; $c23="0"; $c24="0"; $c25="0"; $c26=""; if($distributorcheck==$dealercheck) { $c11="$dealercheck"; $c12="$dealercheck"; $c13="$dealercheck"; $c14="$dealercheck"; $c15="$dealercheck"; $c16="$dealercheck"; } /* else { $result2 = mysql_query("SELECT * FROM smith_prog_item WHERE `item` = $partnumber LIMIT 1"); while ($row2 = mysql_fetch_assoc($result2)) { if($row2["type"]=="NGK") { $c17="100"; $c18="$level6";} elseif ($row2["type"]=="OTHER") { $c11="$level6"; $c12="$level6"; $c13="$level6"; $c14="$level6"; $c15="$level6"; $c16="$level6"; } } } */ $outputstring = $c1. "\t".$c2."\t".$c3."\t".$c4."\t".$c5."\t".$c6."\t".$c7."\t".$c8."\t".$c9."\t".$c10."\t".$c11."\t".$c12."\t".$c13."\t".$c14."\t".$c15."\t".$c16."\t".$c17."\t".$c18."\t".$c19."\t".$c20."\t".$c21."\t".$c22."\t".$c23."\t".$c24."\t".$c25."\t".$c26."\t".$c27."\t".$c28."\t".$c29."\n"; //print $outputstring;echo"<br />"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); echo"<br /><br /><a href=\"http://mysite.com/price/$filename\">Click here to preview file</a>"; ?> Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 Am I setting too many variables? Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 I think I found my problem. I changed $result = mysql_query("SELECT * FROM {$tablename} WHERE $sort > '699' LIMIT 1"); to $result = mysql_query("SELECT * FROM {$tablename} LIMIT 1"); And it processed that row no problem........ The variable $sort is a column... How can I correctly use this query selecting all the rows in the $sort column where the value is 700 or above? Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2009 Share Posted February 26, 2009 How can I correctly use this query selecting all the rows in the $sort column where the value is 700 or above? If you want all rows you don't want to limit the result, also, number in sql (same as in php) are not to be quoted. $result = mysql_query("SELECT * FROM {$tablename} WHERE $sort > 699"); Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted February 26, 2009 Author Share Posted February 26, 2009 thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.