Tips and methods for using HTML to read databases

PHPz
Release: 2024-04-09 13:45:01
Original
711 people have browsed it

为了使用 HTML 从数据库读取数据,有几种方法:使用 AJAX 调用,通过异步通信以无缝方式检索数据;使用 WebSockets,建立持久连接以实现实时数据传输;并且将响应格式化为 JSON,以便轻松客户端解析和处理。

Tips and methods for using HTML to read databases

利用 HTML 读取数据库:技巧与方法

简介

在 Web 应用程序中,从数据库读取数据是至关重要的任务。利用 HTML 作为客户端语言,我们可以轻松地与数据库交互并动态显示数据。本文介绍了几种有效的技巧和方法,帮助你有效地使用 HTML 读取数据库。

AJAX 调用

AJAX (Asynchronous JavaScript and XML) 允许你在不重新加载整个页面的情况下,与服务器进行异步通信。这使得从数据库读取数据变得高效且无缝。以下是使用 AJAX 调用读取数据的代码示例:

function getCustomers() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "get_customers.php");
  xhr.onload = function() {
    if (xhr.status === 200) {
      var customers = JSON.parse(xhr.responseText);
      displayCustomers(customers);
    } else {
      alert("Error fetching customers.");
    }
  };
  xhr.send();
}
Copy after login

此函数通过 AJAX 调用向 get_customers.php 文件发送请求,后者从数据库检索客户数据。响应作为 JSON 格式返回,并在客户端解析和显示。

Web Sockets

Web Sockets 是另一种实现实时通信的强大技术。它允许客户端与服务器建立持久连接,从而可以持续读取数据库数据。以下是用 WebSocket 读取数据库数据的示例代码:

var websocket = new WebSocket("ws://localhost:8080");
websocket.onopen = function() {
  websocket.send("get_customers");
};
websocket.onmessage = function(event) {
  var data = JSON.parse(event.data);
  if (data.type == "customers") {
    displayCustomers(data.customers);
  }
};
Copy after login

此代码使用 WebSocket 对象连接到服务器,并发送一条消息以检索客户数据。服务器处理该请求并通过 WebSocket 连接持续发送数据。

使用 JSON 响应

无论使用 AJAX 还是 Web Sockets,使用 JSON 作为响应格式是一个明智的选择。JSON 是一种轻量级的文本格式,便于解析和处理客户端端。服务器端代码应将数据库数据转换为 JSON 对象,然后将其作为响应返回。

实战案例

任务:创建一个用户列表页面,该页面从数据库动态获取用户数据并显示。

步骤:

  1. 创建一个 get_users.php 文件,该文件用于从数据库中获取用户数据并将其编码为 JSON:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 准备和执行查询
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

// 将结果编码为 JSON
$users = array();
while ($row = $result->fetch_assoc()) {
  $users[] = $row;
}
echo json_encode($users);
?>
Copy after login
  1. 在 HTML 页面中使用 AJAX 调用检索用户数据并将其显示:
<script>
  function getUsers() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "get_users.php");
    xhr.onload = function() {
      if (xhr.status === 200) {
        var users = JSON.parse(xhr.responseText);
        displayUsers(users);
      } else {
        alert("Error fetching users.");
      }
    };
    xhr.send();
  }
</script>

<body onload="getUsers()">
  <div id="user-list"></div>
</body>
Copy after login
  1. 在 HTML 页面中创建 displayUsers() 函数以显示用户数据。

通过遵循这些步骤,你将创建出利用 HTML 从数据库动态读取数据的用户列表页面。

The above is the detailed content of Tips and methods for using HTML to read databases. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!