这段代码可能会产生无限循环吗?

时间:2012-10-20 21:33:45

标签: php

我的页面上的代码似乎没有加载 - 我认为程序逻辑对它来说有点多了。

$resultromney = mysql_query("SELECT category, value, timestamp from results where name='".$chartname2."'");
if (!$resultromney) {
    die('Invalid query, please contact administrator');
}

$posromney = 0;
$negromney = 0;
$dateromney = "1/1/2012";
$romray = array();

while ($twitromney = mysql_fetch_assoc($resultromney)) 
{

$romneytime = intval($twitromney["timestamp"]);
$romneydate = date('m/d/Y', $romneytime);
$romlinearray = array();
$romlinearray["date"] = null;
$romlinearray["value"] = null;
$romneydiff = date_diff($dateromney, $romneydate);

$PLromney = 0;
$NLromney = 0;
$totalromney = 0;

if ($romneydiff == 0) {


switch ($twitromney["category"]) {
    case "composed":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "elated":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "clearheaded":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "agreeable":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "energetic":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "unsure":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "tired":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "depressed":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "guilty":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "confused":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "anxious":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "hostile":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "confident":
        $PLromney = $PLromney + $twitromney["value"];
break;
}
$totalromney = $PLromney - $NLromney;

$romlinearray["value"] = $totalromney + $romlinearray["value"];
}

else {

$dateromney = $romneydate;

switch ($twitromney["category"]) {
    case "composed":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "elated":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "clearheaded":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "agreeable":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "energetic":
        $PLromney = $PLromney + $twitromney["value"];
break;
    case "unsure":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "tired":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "depressed":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "guilty":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "confused":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "anxious":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "hostile":
        $NLromney = $NLromney + $twitromney["value"];
break;
    case "confident":
        $PLromney = $PLromney + $twitromney["value"];
break;
}
$totalromney = $PLromney - $NLromney;

$romlinearray["date"] = $romneydate;
$romlinearray["value"] = $totalromney;
}

$romray[] = $romlinearray;

}

应该计算不同日期的数组,使用不同的数字值,但是页面拒绝加载 - 但是在我将代码放在那里之前它已经加载了。

3 个答案:

答案 0 :(得分:1)

为什么在转换案例中有重复案例?

switch ($twitromney["category"]) {
    case "composed":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "elated":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "clearheaded":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "agreeable":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "energetic":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "unsure":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "tired":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "depressed":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "guilty":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "confused":ss
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "anxious":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "hostile":
    $NLromney = $NLromney + $twitromney["value"];
break;
    case "confident":
    $PLromney = $PLromney + $twitromney["value"];
break;
}

而不是以上使用:

switch ($twitromney["category"]) {
    case "composed":
    case "elated":
    case "clearheaded":
    case "agreeable":
    case "energetic":
    case "confident":
    $PLromney = $PLromney + $twitromney["value"];
break;
    case "unsure":
    case "tired":
    case "depressed":
    case "guilty":
    case "confused":
    case "anxious":
    case "hostile":
    $NLromney = $NLromney + $twitromney["value"];
break;
}

我已将您的代码修改为检查所有DUMPS,请查看下面的ant测试转储:

    $resultromney = mysql_query("SELECT category, value, timestamp from results where name='".$chartname2."'")

    var_dump('[1]:'.$resultromney);

    if (!$resultromney) {
        die('Invalid query, please contact administrator');
    }

    $posromney = 0;
    $negromney = 0;
    $dateromney = "1/1/2012";
    $romray = array();

    while ($twitromney = mysql_fetch_assoc($resultromney)) 
    {

        var_dump('[2]:'.$twitromney);

    $romneytime = intval($twitromney["timestamp"]);

        var_dump('[3]:'.$romneytime);   

    $romneydate = date('m/d/Y', $romneytime);

        var_dump('[4]:'.$romneydate);   

    $romlinearray = array();
    $romlinearray["date"] = null;
    $romlinearray["value"] = null;

        var_dump('[5]:'.$romlinearray); 

    $romneydiff = date_diff($dateromney, $romneydate);

        var_dump('[6]:'.$romneydiff);   

    $PLromney = 0;
    $NLromney = 0;
    $totalromney = 0;

    if ($romneydiff == 0) {

        var_dump('[7]:'.$romneydiff);   


    switch ($twitromney["category"]) {

        var_dump('[8]:'.$twitromney);   

        case "composed":
        case "elated":
        case "clearheaded":
        case "agreeable":
        case "energetic":
        case "confident":
        $PLromney = $PLromney + $twitromney["value"];
    break;
        case "unsure":
        case "tired":
        case "depressed":
        case "guilty":
        case "confused":
        case "anxious":
        case "hostile":
        $NLromney = $NLromney + $twitromney["value"];
    break;
    }

        var_dump('[8]:'.$PLromney);
        var_dump('[9]:'.$NLromney);

    $totalromney = $PLromney - $NLromney;

    $romlinearray["value"] = $totalromney + $romlinearray["value"];

        var_dump('[10]:'.$totalromney, $romlinearray);

    }

    else {

    $dateromney = $romneydate;

    switch ($twitromney["category"]) {

        var_dump('[11]:'.$twitromney);  

        case "composed":
        case "elated":
        case "clearheaded":
        case "agreeable":
        case "energetic":
        case "confident":
        $PLromney = $PLromney + $twitromney["value"];
    break;
        case "unsure":
        case "tired":
        case "depressed":
        case "guilty":
        case "confused":
        case "anxious":
        case "hostile":
        $NLromney = $NLromney + $twitromney["value"];
    break;
    }

        var_dump('[12]:'.$PLromney);
        var_dump('[13]:'.$NLromney);

    $totalromney = $PLromney - $NLromney;

    $romlinearray["date"] = $romneydate;
    $romlinearray["value"] = $totalromney;

        var_dump('[14]:'.$totalromney, $romlinearray);

    }

    $romray[] = $romlinearray;


    var_dump('[15]:'.$romray);

    }

@Tivie说这段代码可以写得更好,是的,这是真的。写得少,做得更多。

答案 1 :(得分:1)

这段代码可能以另一种方式编写。

建议:

使用MySQLi或PDO代替。这是一些documentation regarding Database APIs choices。例如:

$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query( 
   sprint_f("SELECT category, value, timestamp from results where name='%s'", $chartname2) 
);

使用数组存储数据,然后在获得结果后循环它们:

$romneytime = $romneydate = $romlinearray = $romneydiff = array();

while ($twitromney = $result->fetch_assoc()) 
{
    $romneytime[] = intval($twitromney["timestamp"]);
    $romneydate[] = date('m/d/Y', $romneytime);
    $romneydiff[] = date_diff($dateromney, $romneydate);
}

循环每一个,并试图找出哪一个给出错误。 var_dump($ var)派上用场。

答案 2 :(得分:0)

可能是错误。

在/var/logs/apache2/error_log.log上查看你的apache的php日志(或者你正在使用的任何内容)

或类似的东西......