I'm a php beginner and am having a problem finding out why my code keeps throwing up a parser error. Hope you guys can help me out.
The error comes at the end of the code and is "Unexpected , expecting while (T_WHILE)" The code is here...
<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>
Your do-while loop is wrong.
A do-while loop looks like:
do {
/* loop body */
} while (/* condition */);
But in your special case, I'd simply omit the do
keyword:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
/* loop body */
}