title: Retrieve javascript variable from mysql database
body: <p>this variable for locations is working fine in my code.  <a href="http://vince.netau.net" rel="nofollow">http://vince.netau.net</a></p>
  
  <pre><code>var locations = [
    ["John Doe", "145 Rock Ridge Road, Chester, NY ", "41.314926,-74.270134", "http://maps.google.com/mapfiles/ms/icons/blue.png"],
    ["Jim Smith", "12 Williams Rd, Montvale, NJ ", "41.041599,-74.019554", "http://maps.google.com/mapfiles/ms/icons/green.png"],
    ["John Jones", "689 Fern St Township of Washington, NJ ", "40.997704,-74.050598", "http://maps.google.com/mapfiles/ms/icons/yellow.png"],
   ];
  </code></pre>
  
  <p>Now my next step is instead of static data like above, I would like to retrieve data from my mysql database and use it for var locations.  I have this php which is spitting out the data from mysql in exactly the same format as above.
  <a href="http://vince.netau.net/db-connect-test.php" rel="nofollow">http://vince.netau.net/db-connect-test.php</a></p>
  
  <pre><code>&lt;?php
  $servername = "";
  $username = "";
  $password = "";
  $dbname = "";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "SELECT id, name, address, lat, lng, Icon FROM markers";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
      echo '["'  . $row["name"]. '"'.    ', '. '"' . $row["address"].'"'.', '. '"'. $row["lat"].','. $row["lng"].'"'.', '. '"'. $row["Icon"]. '"]'. ','. "&lt;br&gt;";
      }
  } else {
      echo "0 results";
  }
  $conn-&gt;close();
  ?&gt; 
  </code></pre>
  
  <p>I just don't know how to make it work.  Would appreciate any help on how to perform that. I am a beginner.</p>
  
  <p>Thanks</p>
  
  <p>Update-David, I tried the code you wrote. I'm still not sure what this does for me as far as getting the location data in the html?</p>
  
  <pre><code>&lt;?php
      //open connection to mysql db
      $connection = mysqli_connect("","","","") or die("Error " . mysqli_error($connection));
  
      //fetch table rows from mysql db
      $sql = "select * from markers";
      $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
  
      $location = array();;
  if ($result-&gt;num_rows &gt; 0) {
  // output data of each row
  $key = 0;
  while($row = $result-&gt;fetch_assoc()) {
  $locations[$key][] = $row["name"];
  $locations[$key][] = $row["address"];
  $locations[$key][] = $row["lat"];
  $locations[$key][] = $row["lng"];
  $locations[$key][] = $row["Icon"];
  $key++;
  }
  }
  else {
  echo "0 results";
  }
  
  echo json_encode($locations); 
  
  
      //close the db connection
      mysqli_close($connection);
  ?&gt;
  </code></pre>
  
  <p>Here are the results, a little messed up.</p>
  
  <p>[["John Doe","147 Rock Ridge Road, Chester, NY ","41.314926","-74.270134","http://maps.google.com/mapfiles/ms/icons/blue.png"],["Jim Smith","14 Williams Rd, Montvale, NJ ","41.041599","-74.019554","http://maps.google.com/mapfiles/ms/icons/green.png"],["John Jones","691 Fern St Township of Washington, NJ ","40.997704","-74.050598","http://maps.google.com/mapfiles/ms/icons/yellow.png"]]</p>