php - The unstopable Loop X( -
this first code. im trying add new posts wordpress site.
mysql query return 4035 rows have details insert new post.
problem : loop wont ever stop! it reach 4035 row , start on 1st row
things tried make stop:
- added -
if ($loopstimes > 4400) {break;}
- limited mysql query 4050 rows
the weird parts:
- when limit mysql query
limit 900
, works perfectly. loop stops when 900 post added, if add 950, script add 943 posts , give mefatal error "maximum execution time reached"
. script stops. - i increase "maximum execution time" 300 sec , increase memory 128 256mb, use query without limit , loop wont ever stop.
is code problematic? or server issue?
<?php $username = "xxxx"; $password = "xxxx"; $hostname = "localhost"; $loopstimes = 0; //connection database $dbhandle = mysql_connect($hostname, $username, $password) or die("unable connect mysql"); //select database work $selected = mysql_select_db("xxxx_xxxx",$dbhandle) or die("could not select examples"); //execute sql query , return records // query names $result = mysql_query("select distinct names_list.* ... limit 4050"); if($result === false) { die(mysql_error()); // todo: better error handling } //fetch data database while ($row = mysql_fetch_array($result)) { $p_name= $row["p_name"]; $category= $row["gcategory"]; $subcategory= $row["subcategory"]; $ase_date= $row["ase_date"]; //fix (') prepare mysql query $pp_name = $p_name; $newp_name = str_replace("'","''",$p_name); //query count results $minprice = mysql_query("select ... product_p_name = '$newp_name' , order product_price_usd"); $countss = mysql_num_rows($minprice); if($minprice === false) { die(mysql_error()); // todo: better error handling } $rowm = mysql_fetch_array($minprice); $minprice = '$'.$rowm["product_price_usd"]; // create post object $my_post = array( 'post_title' => 'my title...', 'post_status' => 'publish', 'post_author' => 1 ); // insert post database $post_id = wp_insert_post( $my_post ); // add advanced custom field values update_field( "field_552fc3f84b43r", $p_name, $post_id ); //regex date keep year if (preg_match("/20.*/", $ase_date, $matches)) { $ase_year = $matches[0]; } //insert tags custom taxonomies wp_set_post_terms( $post_id, $audio_subcategory, 'subcategory-cpt', false ); wp_set_post_terms( $post_id, $fex_pro_rg, 'fex-pro-cpt', false ); $loopstimes++; if ($loopstimes > 4400) {break;} echo 'post added!, '.$post_id.'<br>'; echo $loopstimes; } //close connection mysql_close($dbhandle); echo 'finish'; ?>
sure know shouldn't use mysql_*
functions anymore.
but pass on issue right now:
//query count results $minprice_query_result = mysql_query("select ... product_p_name = '$newp_name' , order product_price_usd"); $countss = mysql_num_rows($minprice_query_result); if($minprice_query_result === false) { die(mysql_error()); // todo: better error handling } $rowm = mysql_fetch_array($minprice_query_result); $minprice = '$'.$rowm["product_price_usd"]; mysql_free_result ( $minprice_query_result );
and way, why need subquery? $minprice = '$'.$rowm["product_price_usd"];
? never use variable in code!
Comments
Post a Comment