Php 不可分环X(

Php 不可分环X(,php,mysql,wordpress,Php,Mysql,Wordpress,这实际上是我的第一个代码。我正在尝试向我的Wordpress站点添加新帖子。Mysql查询将返回4035行,其中包含插入新帖子的所有详细信息。 问题是:循环永远不会停止!它将到达4035行,然后从第一行重新开始 我试图让它停止的事情: 添加-if($loopstimes>4400){break;} 将mysql查询限制为4050行 奇怪的部分: 当我将mysql查询限制为limit 900时,一切都能完美地运行。当添加900个post时循环停止,如果添加950,脚本将添加943个post,然后

这实际上是我的第一个代码。我正在尝试向我的Wordpress站点添加新帖子。
Mysql查询将返回4035行,其中包含插入新帖子的所有详细信息。
问题是:循环永远不会停止!它将到达4035行,然后从第一行重新开始

我试图让它停止的事情:

  • 添加-
    if($loopstimes>4400){break;}
  • 将mysql查询限制为4050行
  • 奇怪的部分:

  • 当我将mysql查询限制为
    limit 900
    时,一切都能完美地运行。当添加900个post时循环停止,如果添加950,脚本将添加943个post,然后将给我
    致命错误“达到最大执行时间”
    。然后脚本停止
  • 我将“最大执行时间”增加到300秒,并将内存从128增加到256MB,我无限制地使用我的查询,然后循环将永远不会停止
  • 我的代码有问题吗?还是服务器有问题

    <?PHP 
    $username = "xxxx";
    $password = "xxxx";
    $hostname = "localhost"; 
    
    $loopstimes = 0;
    
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
    
    //select a database to work with
    $selected = mysql_select_db("xxxx_xxxx",$dbhandle) 
    or die("Could not select examples");
    
    //execute the SQL query and return records
    // Query to get all names
    $result = mysql_query("SELECT DISTINCT names_list.* ... LIMIT 4050");
    
    if($result === FALSE) { 
        die(mysql_error()); // TODO: better error handling
    }
    
    //fetch the data from the database
    while ($row = mysql_fetch_array($result)) {
        $p_name= $row["p_name"];
        $category= $row["gcategory"];
        $subcategory= $row["subcategory"];
        $ase_date= $row["ase_date"];
    
        //Fix the (') to prepare it for mysql query
    
        $pp_name = $p_name; 
        $newp_name = str_replace("'","''",$p_name);
    
        //Query to count results
        $minprice = mysql_query("SELECT ... WHERE Product_p_name = '$newp_name' AND ORDER BY 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 the post into the database
        $post_id = wp_insert_post( $my_post );
    
        // Add Advanced Custom field values
        update_field( "field_552fc3f84b43r", $p_name, $post_id ); 
    
        //Regex the date to keep just the year
        if (preg_match("/20.*/", $ase_date, $matches)) {
            $ase_year = $matches[0];
        }
    
        //Insert Tags for 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 the connection
    mysql_close($dbhandle);
    echo 'Finish';
    ?>
    

    您肯定已经知道不应该再使用
    mysql.*
    函数了

    但为了帮助你现在就解决这个问题:

    //Query to count results
    $minprice_query_result = mysql_query("SELECT ... WHERE Product_p_name = '$newp_name' AND ORDER BY 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 );
    

    顺便问一下,为什么需要这个子查询?为了得到
    $minprice='$'.$rowm[“product_price_usd”];
    ?但是你从来没有在代码中使用过这个变量!

    我使用XAMPP运行脚本,它工作得很好。对我来说,这意味着我的代码是正确的,问题在于我的主机。 我解决这个问题的方法是限制500,并更新mysql查询以排除已经分配给帖子的名称。然后我运行脚本X次,直到所有名称都分配给帖子

    顺便说一句,我从mysql改为mysqli_*
    谢谢你的建议!

    请。它们不再被维护,现在是。取而代之的是学习,然后使用。首先,尝试使用mysqli,因为mysqli现在已经没有润滑了。Chuck Norris早餐吃的是
    mysql.*
    函数。如果没有看到在
    循环中输入
    的SQL语句,我不确定我们真的能回答这个问题感谢您的时间,$minprice我在自定义字段中使用它。我有多达10个自定义字段,如update_field(“field_552fc3****”,$minprice,$post_id);我从当前代码中删除它们只是为了使其更小。我会尝试你的建议,并尽快告诉你是否有效。你不能发布不完整的代码片段以获得真正的帮助。如果你隐藏部分代码,没有人能帮到你。我确实更改了你的建议,但脚本给了我警告。我下载了整个网站+数据库并将其安装在m上使用xampp的y pc。我使用了我的原始代码,scrip工作得非常完美。所以现在我相信问题出在主机上,知道是什么导致了这种无休止的循环吗?你做了我的建议吗?重命名变量并释放mysql结果?