dongmi4927 2016-11-07 22:26
浏览 55

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

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.

Below is the source code: (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)

<!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"); onwards inside the PHP file. 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.

Any help would be much appreciated.

  • 写回答

1条回答 默认 最新

  • duanjing3656 2019-04-23 22:48
    关注
    mysqli_select_db($con,"ajax_demo");
    

    Replace the "ajax_demo" with your database name.

    评论

报告相同问题?

悬赏问题

  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题