0

I am a php beginner and in order to create a chart I need a php file with json data, which is created through a database query. My query code looks as follows:

<?php

$servername = "xyz";

$username = "x";
$password = "y";
$dbname = "z";


// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);

$data1 = [];



$sql = "SELECT ROUND value, timestamp FROM table";
$result = mysqli_query($mysqli, $sql);
while ($row = mysqli_fetch_array($result)) {
    array_push($data1,[intval($row['timestamp']),doubleval($row[0])]);
}

?>

Now I want to display the code by printing the query so it has a json format like:

[ [12000000000000,5.5],
  [12000000000000,5.5],
  .......
]

I tried several things like print_r etc. but nothing quite got me there.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Jannik732
  • 41
  • 1
  • 3
  • 1
    why not utalize the `json_encode` function within php? https://www.php.net/manual/en/function.json-encode.php – Dorvalla Apr 21 '20 at 13:26
  • I did exactly do that but it did not print anything. I am a beginner in php and would need a code example on how to exactly put the Syntax – Jannik732 Apr 21 '20 at 13:45
  • and would you still be so kind to reopen the question, as the other ones are not exactly helping in this problem. I tried it out but the cases are a bit different – Jannik732 Apr 21 '20 at 13:47
  • 1
    Rather than asking for “examples” (and on something as minor as a function call), you should show us what _you_ tried, so that we can figure out what you did wrong. – CBroe Apr 21 '20 at 13:52
  • As mentioned i tried the print_r approach: print_r(json_encode($data1)); This generated the output similarly to what i need (see above) but it adds a ";" at the end of the output and the file cannot be interpreted by the chart. Furthermore, it does not create line breaks after each row, however, i dont know if these are really necessary or just cosmetic – Jannik732 Apr 21 '20 at 14:01

0 Answers0