title: MySQL data from PHP to Javascript with JSON
body: <p>I am trying to use a code that will take data from a mySQL database bind that data to a variable, put all the resulting $x into a PHP array, and finally convert it to JSON format. I then take the JSON into javascript to handle the data from the data base there.</p>
  
  <p>Please see my code:</p>
  
  <pre><code>&lt;?php
          //bind to $x
          $mysqli = new mysqli('localhost', 'root', 'root', 'mytable');
          if ($stmt = $mysqli-&gt;prepare("SELECT x FROM data")) {
              $stmt-&gt;bind_result($x);
              $OK = $stmt-&gt;execute();
          }
          //put all of the resulting $x into a PHP array
          $result_array = Array();
          while($stmt-&gt;fetch()) {
              $result_array[] = $x;
          }
          //convert the PHP array into JSON format, so it works with javascript
          $json_array = json_encode($result_array);
  
          if ($stmt = $mysqli-&gt;prepare("SELECT data.y FROM data")) {
              $stmt-&gt;bind_result($y);
              $OK = $stmt-&gt;execute();
          }
          //put all of the resulting y into a PHP array
          $result_array = Array();
          while($stmt-&gt;fetch()) {
              $result_array[] = $y;
          }
          //convert the PHP array into JSON format, so it works with javascript
          $json_array2 = json_encode($result_array);  
  
  
      ?&gt;  
  
      &lt;script&gt;
          var xv = &lt;?php echo $json_array; ?&gt;;
          var yv = &lt;?php echo $json_array2; ?&gt;;
          var storage = [];
          for(var i=0;i&lt;100;i++)
          { 
              var x = xv[i];
              var y = yv[i];
              var json = {x: x, y: y};
              storage.push(json); 
          }
  </code></pre>
  
  <p>My question is why is the page displaying this as an output and not transferring the data to the arrays</p>
  
  <p>"prepare("SELECT x FROM data")) { $stmt->bind_result($x); $OK = $stmt->execute(); } //put all of the resulting names into a PHP array $result_array = Array(); while($stmt->fetch()) { $result_array[] = $x; } //convert the PHP array into JSON format, so it works with javascript $json_array = json_encode($result_array); /* if ($stmt = $mysqli->prepare("SELECT data.y FROM data")) { $stmt->bind_result($y); $OK = $stmt->execute(); } //put all of the resulting names into a PHP array $result_array = Array(); while($stmt->fetch()) { $result_array[] = $y; } //convert the PHP array into JSON format, so it works with javascript $json_array2 = json_encode($result_array);  */ ?>"</p>