【问题标题】:The unstopable Loop X(不可阻挡的循环 X(
【发布时间】:2023-03-23 09:40:02
【问题描述】:

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

我试图让它停止的事情:

  1. 已添加 - if ($loopstimes > 4400) {break;}
  2. 将 mysql 查询限制为 4050 行

奇怪的部分:

  1. 当我将 mysql 查询限制为 LIMIT 900 时,一切正常完美。当添加 900 个帖子时循环停止,如果我添加 950,脚本将添加 943 个帖子,然后会给我Fatal error "Maximum execution time reached"。然后脚本停止。
  2. 我将“最大执行时间”增加到 300 秒,并将内存从 128 增加到 256MB,我使用没有 LIMIT 的查询,然后循环永远不会停止。

我的代码有问题吗?还是服务器问题?

<?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';
?>

【问题讨论】:

标签: php mysql wordpress


【解决方案1】:

当然你已经知道你不应该再使用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"]; ?但你永远不会在代码中使用该变量!

【讨论】:

  • 感谢您的宝贵时间,我在自定义字段中使用了 $minprice。我有多达 10 个自定义字段,例如 update_field("field_552fc3*****", $minprice, $post_id ); 我从当前代码中删除它们只是为了让它更小。我会试试你的建议,如果能尽快告诉你。
  • 您不能发布不完整的代码片段以获得真正的帮助。如果您隐藏部分代码,没有人可以提供帮助。
  • 我确实改变了你的建议,但脚本给了我警告。我下载了整个网站 + dbases 并使用 xampp 将其安装在我的电脑上。我使用了我的原始代码,脚本完美。所以现在我认为问题出在主机上,知道什么可能导致这个无限循环吗?
  • 你做了我的建议吗?重命名变量并释放 mysql 结果?
【解决方案2】:

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

顺便说一句,我从 mysql_* 更改为 mysqli_* 感谢您的建议!

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2013-07-10
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多