title: Pass mysql table values to Javascript via PHP
body: <p>I am trying to take all of the rows MYSQL table and ultimately have them end up in an interactive HTML table. I currently have no problem inserting values into my table using a $.post function, I just cannot return them, at least, not all of them. </p>
  
  <p>Here is my JS code:</p>
  
  <pre><code>function load() {   
  
              $.post(
              "Returnsmedb.php",
              function (response) {
                  var firstname = response.fname;
                  var lastname = response.lname;
                  var username = response.uname;
                  var email = response.email;
                  var password = response.password;
  
                  console.log(response);
              }, 'JSON'
          );  
      }
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  header('Content-type: application/json');
  
  $servername = "localhost";
  $username = "SME";
  $password = "mypass";
  $db = "p3";
  
  // Create connection
  $conn =  new mysqli($servername, $username, $password, $db);
  
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  
  $sql = "SELECT * FROM pendingusers";
  $result = $conn-&gt;query($sql);
  $response = array();
  
  if ($result-&gt;num_rows &gt; 0) {
  
   while($row = $result-&gt;fetch_array()) {
      // $response[] = $row[];
      $response['fname'] = $row["fname"];
      $response['lname'] = $row["lname"];
      $response['uname'] = $row["uname"];
      $response['email'] = $row["email"];
  
  }
      echo json_encode($response);
  } else {
      echo "0 results";
  }
  $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>I am thinking the easiest thing to do would be to store each row in an array, but I am not sure. It currently returns my $response array, but all of the values are null, except for 'email', which has the correct value, but it is from the last row of my DB table. Also, it only returns one row. </p>
  
  <p>Any help / guidance would be much appreciated.</p>
  
  <p>Thanks!</p>