我不完全理解为什么这个 PHP 代码没有将数据保存在数组中。当我在 while 循环期间回显数组时,会设置值,但在退出循环后,数组中的值会丢失。离开数组后,其他变量确实包含它们的值。
<?php
class VIEWARRAY {
public $year;
public $month;
public $day = array();
public $views = array();
}
$viewdatabase = array();
$connection = MySQL_Connection($host, Session_Get("username"), Session_Get("password"), $database);
$dayviewindex = 0; $monthyearindex = 0; $previousmonth = "";
$information = MySQL_Script($connection, "SELECT * FROM viewdatabase");
while ($index = mysqli_fetch_array($information)) {
if ($index["month"] != $previousmonth) {
$viewdatabase[$monthyearindex] = new VIEWARRAY;
$viewdatabase[$monthyearindex]->year = $index["year"];
$viewdatabase[$monthyearindex]->month = $index["month"];
$dayviewindex = 0;
$monthyearindex++;
$previousmonth = $index["month"];
}
$viewdatabase[$monthyearindex]->day[$dayviewindex] = $index["day"];
$viewdatabase[$monthyearindex]->views[$dayviewindex] = $index["views"];
$dayviewindex++;
}
MySQL_Disconnect($connection);
//testing area
echo "->" . $viewdatabase[0]->year . " + " . $viewdatabase[0]->month . "<br />"; //prints out ->2013 + 8
echo "------>" . $viewdatabase[0]->day[2] . " + " . $viewdatabase[0]->views[2] . "<br />"; //prints out ------> +
echo count($viewdatabase[0]->views) . "<br />"; //prints out 0
echo count($viewdatabase[1]->views) . "<br />"; //prints out 0
?>
我确保我能够很好地连接到我的数据库并且我的数据库确实返回了信息。这就是我的数据库的设置方式
year month day views
2013 8 25 1
2013 8 26 1
2013 8 27 1
2013 9 3 1