2

如何执行该循环:

while ($row = mysql_fetch_array($result)) {
    collectData($row['NAME']);
}

30 秒后不冻结我的 PHP?

我认为可以通过将整个数据库放入数组或 *.temp 文件中来处理它,而不是每次我想输入/输出某些东西时都连接。但是我在文件和数组方面遇到了问题,所以这就是我要问的原因:什么是更好/最好的方法来快速、轻松地完成它?

<?php

//fiveMin - Database that data is taken form
//HighCharts - Database that data is transferred to

$fiveMin=mysql_connect($fiveMin_host,$fiveMin_user,$fiveMin_pass);
mysql_select_db($fiveMin_db,$fiveMin) or die (mysql_error());               

$query="SELECT * FROM BetterShopItemStock";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
    collectData($row['NAME']);
}

function collectData($itemID) {

    global $fiveMin_host, $fiveMin_user, $fiveMin_pass, $fiveMin_db, $week_host, $week_user, $week_pass, $week_db;  

    $fiveMin=mysql_connect($fiveMin_host,$fiveMin_user,$fiveMin_pass); //Load and store data from fiveMin Database
    mysql_select_db($fiveMin_db,$fiveMin) or die (mysql_error());
    $function_Query="SELECT AMT FROM BetterShopItemStock WHERE NAME = '$itemID'";
    $function_Ask = mysql_query($function_Query);
    $function_Result = mysql_fetch_row($function_Ask, 0);
    $dataReadyToImport = "," . $function_Result[0];
    @mysql_close($fiveMin);

    $HighCharts=mysql_connect($week_host,$week_user,$week_pass); //Save stored data to weekly Database
    mysql_select_db($week_db,$HighCharts) or die (mysql_error());
    $function_Query="SELECT AMT FROM BetterShopItemStock WHERE NAME = '$itemID'";
    $function_Ask = mysql_query($function_Query);
    $function_Result = mysql_fetch_row($function_Ask, 0);
    $storedData = $function_Result[0];
    $dataReadyToImport = $storedData . $dataReadyToImport;
    mysql_query("UPDATE BetterShopItemStock SET AMT='$dataReadyToImport' WHERE NAME='$itemID'");

    @mysql_close($HighCharts);

}

?>
4

2 回答 2

1

(1) 您不必为每个查询打开和关闭一个连接 - 打开一个到每个服务器的连接并对mysql_query()它们进行多次调用。这应该会加快你的速度。

(2) 不要使用 "SELECT * FROM BetterShopItemStock" ...只选择您需要的行。(此处为“名称”)。如果您已经在该表中查询“NAME”,请同时选择“AMT”,而不是再次调用它。

(3) 您可以将对 HighCharts 的 select 和 update 调用合并到一个查询中。

总而言之,一切都应该是这样的:

<?php

    //fiveMin - Database that data is taken form
    $fiveMin=mysql_connect($fiveMin_host,$fiveMin_user,$fiveMin_pass);
    mysql_select_db($fiveMin_db,$fiveMin) or die (mysql_error());

    //HighCharts - Database that data is transferred to
    $HighCharts=mysql_connect($week_host,$week_user,$week_pass); //Save stored data to weekly Database
    mysql_select_db($week_db,$HighCharts) or die (mysql_error());

    $query="SELECT NAME,AMT FROM BetterShopItemStock";
    $result = mysql_query($query,$fiveMin);
    while ($row = mysql_fetch_row($result)) {
        collectData($row[0],$row[1],$HighCharts);
    }

    function collectData($itemID, $itemAmt, $mysql) {
        $id = mysql_real_escape_string($itemID);
        $amt = mysql_real_escape_string($itemAmt);
        $q = "UPDATE BetterShopItemStock SET ".
             "AMT=CONCAT(AMT,',','$amt') WHERE NAME='$id'";
        $r = mysql_query($q,$mysql);
        return (!$r ? false : true);


    }

    //now close the databases
    @mysql_close($HighCharts);
    @mysql_close($fiveMin);
?>

(4) 最后,如果您遇到执行超时错误,请查看 PHP 的set_time_limit以延长您的执行时间

于 2012-07-20T00:37:25.267 回答
0

将每个人分成班级,然后与他们一起工作。它使它们更有效地运行,并且查询不会相互交织:

<?php
class class1{
     function fivemin(){
          **code0**
     }
     function collectdata1-fivemin() {
          class1::fivemin();
          **code**
     }
     function collectdataHighcharts(){
          class1::fivemin();
          **code2**
     }
}
$a = new class1;
$a->collectdata1-fivemin();
$a->collectdata1HighCharts();
?>
于 2012-07-20T00:43:25.947 回答