-1

我是 php 初学者,在找出为什么我的代码不断抛出解析器错误时遇到了问题。希望你们能帮帮我。

错误出现在代码的末尾,并且是“Unexpected ,expecting while (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>
4

1 回答 1

2

你的 do-while 循环是错误的。

do-while 循环如下所示:

 do {
     /* loop  body */
 } while (/* condition */);

但在您的特殊情况下,我会简单地省略do关键字:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   /* loop body */
}
于 2013-04-20T16:42:47.483 回答