Hi guys can't seem to use MySQL row values in JavaScript. I've tried all kinds of quote and double-quote combinations.
$sql = "SELECT desk_id, desk_x, desk_y FROM desks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<script type="text/javascript">';
echo 'new Desk('$row["desk_id"]', '$row["desk_x"]', '$row["desk_y"]')';
echo '</script>';
}
}
I'm getting this error:
Parse error: syntax error, unexpected '$row' (T_VARIABLE), expecting ',' or ';'
Thanks for any help.
Edit: Thanks for the answers guys.
This works:
while($row = $result->fetch_assoc()) {
echo '<script type="text/javascript">';
echo "new Desk('{$row["desk_id"]}', '{$row["desk_x"]}', '{$row["desk_y"]}')";
echo '</script>';
}
When I try to put the open and close tags outside of the while loop I get the error:
SyntaxError: Unexpected token new