【问题标题】:php script hanging for a large data set为大型数据集挂起的 php 脚本
【发布时间】:2017-09-05 05:01:32
【问题描述】:

我正在建立一个网站,它从另一个网站抓取数据,将其存储在数据库中并以表格的形式显示。只要行数较少(大约 100 行),一切正常,但是当数据集增加时,比如 300 行或更多行,数据会存储在数据库(phpmyadmin)中,但屏幕上什么都没有显示,网站只是保持加载。下面是我正在运行的 php 脚本的一部分:

<?php

// configuration
require("../includes/helpers.php"); 

        // initializing current page and number of pages
        $page = 0;
        $pages = 1;

        // scrape data from each page
        while($pages--)
        {
            // next page
            $page++;

            // scrape data from shiksha.com
            $string = @file_get_contents("http://www.shiksha.com/b-tech/colleges/b-tech-colleges-".urlencode($_POST["city"])."-{$page}");

            if($string === false)
                apologize("Please enter a valid city name");

            if($page === 1)
            {
                // counting total number of pages
                preg_match_all('/class=" linkpagination">/',$string,$result);
                $pages = sizeof($result[0]);
            }

            // passing the string for scraping data  and storing in database
            get_college_info($string,$page);

            // delay for 2s
            sleep(2);
        } 


        // querying the infrastructure table for facilities of all colleges
        $infra = query("SELECT college_id,facilities FROM infrastructure ");

        // preparing query and selecting data from table college_info
        $result = query("SELECT * FROM college_info");

        // render(output) results
        render("result.php",["title" => "result","infra" => $infra,"result" => $result]);
    }
}?>

有趣的是,如果我已经将数据存储在我的数据库中并且我只是检索并打印它,那么一切正常并且所有数据,无论它有多大,都会被打印出来。我不知道有什么问题。 PS:我已经尝试过 set_time_limit()。

【问题讨论】:

    标签: php html mysqli phpmyadmin


    【解决方案1】:

    您正在创建一个无限循环。因此,要解决此问题,请将 while 循环的条件更改为以下内容。

    while($page<$pages)
    {
        //your same code here
    }
    

    【讨论】:

    • 我不认为这是问题所在,反正我试过了,但还是不行。 @user3299379
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2020-09-21
    • 2015-09-20
    • 1970-01-01
    • 2015-03-01
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多