简体   繁体   English

使用PHP和AJAX从MySQL数据库中选择和检索数据

[英]Selecting and retrieving Data from a MySQL Database using PHP and AJAX

I'm trying to select data from a MySQL database that is hosted on a webserver. 我正在尝试从Web服务器上托管的MySQL数据库中选择数据。 I want to be able to retrieve the data from a table within the database and then illustrate it within a HTML table. 我希望能够从数据库中的表中检索数据,然后在HTML表中进行说明。 There's an example on W3Schools that I've been following, but I'm unable to retrieve the data successfully. 我一直在关注W3Schools上的一个示例,但是我无法成功检索数据。

Below is the source code: (HTML) 以下是源代码:(HTML)

<html>
<head>
//Javascript code
<script>
       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 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
        }
     };
       xmlhttp.open("GET","getuser.php?q="+str,true);
       xmlhttp.send();
 }
}
</script>

</head>
<body>

<form>
 <select name="users" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2">Lois Griffin</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

PHP File: (getuser.phd) PHP文件:(getuser.phd)

<!DOCTYPE html>
<html>
<head>
<style>
  table            {
  width: 100%;
  border-collapse: collapse;
        }

     table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

     th {text-align: left;}
</style>
</head>
<body>

<?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 "<table>
<tr>
 <th>Firstname</th>
 <th>Lastname</th>
 <th>Age</th>
 <th>Hometown</th>
 <th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
}
  echo "</table>";
  mysqli_close($con);
?>
</body>
</html>

I think the issue might exist from mysqli_select_db($con,"ajax_demo"); 我认为问题可能出在mysqli_select_db($ con,“ ajax_demo”);中。 onwards inside the PHP file. 在PHP文件中。 Should I be referring to the table that contains the data inside the database? 我应该引用包含数据库内部数据的表吗?

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. 我的Web服务器上托管了PHP文件,因此我不确定为什么从HTML页面上的选项列表中选择一个人时为什么它不检索该数据。

Any help would be much appreciated. 任何帮助将非常感激。

mysqli_select_db($con,"ajax_demo");

用您的数据库名称替换"ajax_demo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM