我是一个php初学者,我找不到为什么我的代码不断抛出解析器错误。希望你们能帮助我。
错误出现在代码的末尾,并且是“意外的,期待的是(T_WHILE)” 代码就在这里......
<html>
<?php
$db = new PDO('mysql:host=localhost;dbname=testit;charset=UTF-8', 'testit', 'testit', array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$stmt = $db->query("SELECT * FROM TestTable");
$amdata = array();
$ctrinner = 0;
$ctrouter = -1;
$prevweek = "9999";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) do {
if ($prevweek !== $row['WeekNumber']) {
$ctrouter++;
$ctrinner = 0;
$amdata[$ctrouter][$ctrinner] = 'week: "'+$row['WeekNumber']+'"';
}
$ctrinner++;
$amdata[$ctrouter][$ctrinner] = $row['XAxisCategory']+': '+$row['YAxisValue'];
$prevweek = $row['WeekNumber'];
}
// Whatever comes aftert this line throws a php error 'Unexpected <whatever I put there>, expecting while (T_WHILE)....'
// Even if I put nothing it complains about Unexpected '?>' expecting while (T_WHILE) ?>
?>
</html>
答案 0 :(得分:2)
你的do-while循环错误。
do-while循环如下所示:
do {
/* loop body */
} while (/* condition */);
但在您的特殊情况下,我只是省略do
关键字:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
/* loop body */
}