生成的数组值在While语句后更改

时间:2014-03-24 01:04:35

标签: php

这是我遇到的一个奇怪的问题,我想知道是否有人有我的洞察力。不确定mktime是否无法正常运行,因为我试图将其发挥作用,或者可能会发生什么。

昨晚,事情进展顺利 - 显示的月份是正确的。但是,今天,由于某些原因,我的$aGMonV的值在foreach之后和while(row_* = mysqli_fetch_array*语句之前的某处发生了变化。

虽然var_dump返回%2014-03%作为第一个月(这是正确的) - 生成的表作为第一个月返回%2013-09%。正在运行的所有查询都使用%2013-09%运行,而不是从当月开始。

我的代码如下:

$aGMon = array();

for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }

foreach ($aGMon as $aGMonK => $aGMonV)

{

$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";

$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " .     $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV     . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as     Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData     from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill     LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF     from CF6MCI where Date LIKE " . $aGMonV . ";");

var_dump($aGMonV);

while($row_E = mysqli_fetch_array($result_E))
while($row_G = mysqli_fetch_array($result_G))
while($row_P = mysqli_fetch_array($result_P))
while($row_T = mysqli_fetch_array($result_T))

{

echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: "     . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";

}

}

代码结果如下:

Result of code

1 个答案:

答案 0 :(得分:2)

user3260912尝试删除while,尝试这样:

$aGMon = array();

for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }

foreach ($aGMon as $aGMonK => $aGMonV)

{

$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";

$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " .     $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV     . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as     Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData     from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill     LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF     from CF6MCI where Date LIKE " . $aGMonV . ";");

//var_dump($aGMonV);

$row_E = mysqli_fetch_array($result_E);
$row_G = mysqli_fetch_array($result_G);
$row_P = mysqli_fetch_array($result_P);
$row_T = mysqli_fetch_array($result_T);



echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: "     . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";



}