title: Trying to return multiple rows from database json encode
body: <p>i am working on a little something, i have snipped together code from all over the place and am having trouble to get it to work.</p>
  
  <p>So what im trying to do is have one page request another via ajax/json/php and query a database and then return the multiple results and print it out on the first page.</p>
  
  <p>My problem is that whenever i run the client.php page, i only get back </p>
  
  <p>id: [object Object] name: [object Object]</p>
  
  <p>Instead of the data that i want, i know it is probably just a silly little mistake somewhere so any help would be appreciated. </p>
  
  <p>And before i get grilled for using mysql instead of mysqli, its just how the code came that im using from <a href="http://openenergymonitor.org/emon/node/107" rel="nofollow">http://openenergymonitor.org/emon/node/107</a></p>
  
  <p>Client.php </p>
  
  <pre><code>    &lt;html&gt;
      &lt;head&gt;
           &lt;script language="javascript" type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;h2&gt; Client example &lt;/h2&gt;
      &lt;h3&gt;Output: &lt;/h3&gt;
          &lt;div id="output"&gt;this element will be accessed by jquery and this text    replaced&lt;/div&gt;
  
      &lt;script id="source" language="javascript" type="text/javascript"&gt;
  
      $(function () 
      {
      $.ajax({                                      
      url: 'api.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to   api.php
                                     //for example "id=5&amp;parent=6"
      dataType: 'json',                //data format      
  
    success: function(data)          //on recieve of reply
      {
  
       var id = data[0];              //get id
       var vname = data[1];           //get name
  
       $('#output').html("&lt;b&gt;id: &lt;/b&gt;"+id+"&lt;b&gt; name: &lt;/b&gt;"+vname); //Set output element html
  
       }
       });
       }); 
  
       &lt;/script&gt;
       &lt;/body&gt;
       &lt;/html&gt;
  </code></pre>
  
  <p>Api.php</p>
  
  <pre><code>    &lt;?php 
  
       $host = "localhost";
       $user = "root";
       $pass = "";
       $databaseName = "abeiq_stock";
       $tableName = "variables";
  
       $con = mysql_connect($host,$user,$pass);
       $dbs = mysql_select_db($databaseName, $con);
  
  
       $query = "SELECT * FROM $tableName";
       $result = mysql_query($query);
  
       $rows = array();
  
       while($r = mysql_fetch_assoc($result)){
       $rows[] = $r; has the same effect, without the superfluous data attribute
       $rows[] = array('data' =&gt; $r);
        }
  
         echo json_encode($rows);
  
       ?&gt;
  </code></pre>