I feel like banging my head now. I can't solve it.. doesn't make sense. $sql = "SELECT x,y FROM datapoints "; $result = $conn->query($sql) ; if ($result->num_rows > 0) { // output data of each row $dataPoints = array( while($row = $result->fetch_assoc()) { array("x" => $row["x"], "y" => $row["y"]) if ($result->num_rows > 1) { echo ",";} } ); } else { echo "0 results"; } Code (markup): I'm getting this error for this code My expected outcome is somthing like this
Sometimes it can be good to use a "tidy" or "format" tool to show you where you've gone wrong. I don't use netbeans for day to day editing but it's perfect for the kind of thing you're doing here (and you'll kick yourself when you see it). My real comment is more about how you are trying to create that javascript array. What you have as your expected array isn't going to play nicely. Then again you could be doing something else altogether and I've got the wrong end of the stick. Try this instead - untested but should be more or less right <?php $sql = "SELECT x,y FROM datapoints "; $result = $conn->query($sql) ; if ($result->num_rows > 0) { // output data of each row $dataPoints = array(); while($row = $result->fetch_assoc()) { $dataPoints[] = "{$row['x']} : {$row['y']}"; ); echo 'var dataPoints = {'.implode(' , ', $dataPoints).'}'; } else { echo "0 results"; } PHP:
Thanks but this one didn't work.. however this code worked that I got from someone.. if ($result->num_rows > 0) { // output data of each row while ($row = $result->fetch_assoc()) { $dataPoints[] = array("x" => $row["x"], "y" => $row["y"]); } } else { echo "0 results"; } Code (markup): I appreciate your help.
It is going to be very hard for you to learn PHP if you do not understand the basic syntax, I suggest you take a few hours to cover them @ http://php.net/manual/en/language.basic-syntax.php
Also, you tried to directly assign array-values with a while-loop inside an array-declaration... So no, you don't know basic syntax.