title: Dynamically Create JavaScript Object From MySQL Table
body: <p>Lets say I have this table <code>mytable</code> </p>
  
  <p><code>id | name | x | y</code></p>
  
  <p>I pull the the rows from <code>mytable</code> and create JavaScript objects with it like so:</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>$sql = "SELECT * FROM mytable";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      while($row = $result-&gt;fetch_assoc()) {  
          echo '&lt;script type="text/javascript"&gt;';
          echo "new Object({$row["id"]}, '{$row["name"]}', {$row["desk_x"]}, {$row["desk_y"]});";
          echo '&lt;/script&gt;';            
      }
  }
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code>function Object(id, name, x, y) {
      var obj = {
          id:id, 
          name:name, 
          x:x, 
          y:y
      };
  }
  </code></pre>
  
  <p>At the moment this is fine but lets say I want to add another column <code>color</code> to <code>mytable</code></p>
  
  <p>Basically I'm asking what do I write in PHP and JS to make this object dynamically, so you can have any columns and the <code>Object</code> object will just add a new property with the name of the column?</p>