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

时间:2016-11-07 22:26:39

标签: javascript php html mysql ajax

我正在尝试从托管在网络服务器上的MySQL数据库中选择数据。我希望能够从数据库中的表中检索数据,然后在HTML表中进行说明。 W3Schools上有一个我一直关注的例子,但是我无法成功检索数据。

以下是源代码:(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文件:(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>

我认为问题可能存在于PHP文件中的 mysqli_select_db($ con,“ajax_demo”); 之后。我应该引用包含数据库内数据的表吗?

我在我的网络服务器上托管了PHP文件,因此我不确定当从HTML页面上的选项列表中选择某人时,为什么它不会检索该数据。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

100

用您的数据库名称替换 # now we will get the sum of the peaks in the given image # To do this we use opencv's cornerHarris function. # this function gives the corners in an image which in our case is peaks. # we get the coordinates of these peaks through the cornerHarris function, # and we subtract it from the coordinates of mid line in order to find the # height of the peak. Then we add all these heights to get the sum of the # heights which is our feature for classifying the image. def give_peak_sum(file): image = cv2.imread(file) # opencv's image read function image_copy = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # converts image from # BGR color space to RGB color space image_dims = image.shape x_dim = image_dims[1] # converting to gray scale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = np.float32(gray) # detect corners dst = cv2.cornerHarris(gray, 2, 3, 0.04) # dilate corner image to enhance corner points dst = cv2.dilate(dst, None) thresh = 0.02*dst.max() peak_sum = 0 mid_line = get_mid_line(image_copy) # using previously defined function for j in range(0, dst.shape[0]): for i in range(0, dst.shape[1]): if (dst[j, i] > thresh and i < 0.4*x_dim): peak_sum += abs(j-mid_line) return (peak_sum)