title: Selecting and retrieving Data from a MySQL Database using PHP and AJAX
body: <p>I'm trying to select data from a MySQL database that is hosted on a webserver. I want to be able to retrieve the data from a table within the database and then illustrate it within a HTML table. There's an example on W3Schools that I've been following, but I'm unable to retrieve the data successfully.</p>
  
  <p>Below is the source code: (HTML)</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  //Javascript code
  &lt;script&gt;
         function showUser(str) {
             if (str == "") {
                 document.getElementById("txtHint").innerHTML = "";
                 return;
         } else { 
             if (window.XMLHttpRequest) {
             // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
        } else {
            //  code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
          xmlhttp.onreadystatechange = function() {
          if (this.readyState == 4 &amp;&amp; this.status == 200) {
              document.getElementById("txtHint").innerHTML = this.responseText;
          }
       };
         xmlhttp.open("GET","getuser.php?q="+str,true);
         xmlhttp.send();
   }
  }
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form&gt;
   &lt;select name="users" onchange="showUser(this.value)"&gt;
    &lt;option value=""&gt;Select a person:&lt;/option&gt;
    &lt;option value="1"&gt;Peter Griffin&lt;/option&gt;
    &lt;option value="2"&gt;Lois Griffin&lt;/option&gt;
    &lt;option value="3"&gt;Joseph Swanson&lt;/option&gt;
    &lt;option value="4"&gt;Glenn Quagmire&lt;/option&gt;
  &lt;/select&gt;
  &lt;/form&gt;
  &lt;br&gt;
  &lt;div id="txtHint"&gt;&lt;b&gt;Person info will be listed here...&lt;/b&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP File: (getuser.phd)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
    table            {
    width: 100%;
    border-collapse: collapse;
          }
  
       table, td, th {
              border: 1px solid black;
              padding: 5px;
          }
  
       th {text-align: left;}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
       $q = intval($_GET['q']);
  
  $con = mysqli_connect(‘www.example.com’,’user_Admin’,’12345-678’,’my_DB');
  if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
  }
  
  mysqli_select_db($con,"ajax_demo");
  $sql="SELECT * FROM user WHERE id = '".$q."'";
  $result = mysqli_query($con,$sql);
  
    echo "&lt;table&gt;
  &lt;tr&gt;
   &lt;th&gt;Firstname&lt;/th&gt;
   &lt;th&gt;Lastname&lt;/th&gt;
   &lt;th&gt;Age&lt;/th&gt;
   &lt;th&gt;Hometown&lt;/th&gt;
   &lt;th&gt;Job&lt;/th&gt;
  &lt;/tr&gt;";
  while($row = mysqli_fetch_array($result)) {
    echo "&lt;tr&gt;";
    echo "&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['LastName'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Age'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Hometown'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Job'] . "&lt;/td&gt;";
    echo "&lt;/tr&gt;";
  }
    echo "&lt;/table&gt;";
    mysqli_close($con);
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I think the issue might exist from  <strong>mysqli_select_db($con,"ajax_demo");</strong> onwards inside the PHP file. Should I be referring to the table that contains the data inside the database?</p>
  
  <p>I have the PHP File hosted on my webserver, so I'm not sure why it won't retrieve that data when a person is selected from the list of options on the HTML page.</p>
  
  <p>Any help would be much appreciated. </p>