php - 使用 PHP/Jquery 显示从 Mysql 数据库到 Html5 bootstrap 的值

标签 php mysql mysqli bootstrap-4

这是我的代码

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Control Panel</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="css/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="css/green.css" rel="stylesheet">
    <!-- Datatables -->
    <link href="css/dataTables.bootstrap.min.css" rel="stylesheet">
    <link href="css/buttons.bootstrap.min.css" rel="stylesheet">
    <link href="css/fixedHeader.bootstrap.min.css" rel="stylesheet">
    <link href="css/responsive.bootstrap.min.css" rel="stylesheet">
    <link href="css/scroller.bootstrap.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="css/custom.min.css" rel="stylesheet">
  </head>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-12 left_col">
          <div class="left_col scroll-view">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_content">
                    <table id="datatable-buttons" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>S/N</th>
                          <th>Date</th>
                          <th>IP Address</th>
                          <th>Activation</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <td>get data from db</td>
                          <td>get data from db</td>
                          <td>get data from db</td>
                          <td>get data from db</td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>

    <!-- jQuery -->
    <script src="js/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="js/fastclick.js"></script>
    <!-- NProgress -->
    <script src="js/nprogress.js"></script>
    <!-- iCheck -->
    <script src="js/icheck.min.js"></script>
    <!-- Datatables -->
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap.min.js"></script>
    <script src="js/dataTables.buttons.min.js"></script>
    <script src="js/buttons.bootstrap.min.js"></script>
    <script src="js/buttons.flash.min.js"></script>
    <script src="js/buttons.html5.min.js"></script>
    <script src="js/buttons.print.min.js"></script>
    <script src="js/dataTables.fixedHeader.min.js"></script>
    <script src="js/dataTables.keyTable.min.js"></script>
    <script src="js/dataTables.responsive.min.js"></script>
    <script src="js/responsive.bootstrap.js"></script>
    <script src="js/dataTables.scroller.min.js"></script>
    <script src="js/jszip.min.js"></script>
    <script src="js/pdfmake.min.js"></script>
    <script src="js/vfs_fonts.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="js/custom.min.js"></script>

  </body>
</html>

PHP

<?php
$servername = "remotedbIP";
$username = "root";
$password = "Password";
$dbname = "installation";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, date, ip, activate FROM software";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - date: " . $row["date"]. " - ip: " . $row["ip"].  " - activate: " . $row["activate"]. " <br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

我正在尝试使用带有 Bootstrap 的 html 表格格式显示 php 文件检索到的数据。我不知道该怎么做。我试着遵循一些指导方针,但我想不通。任何指向正确指南或帮助的方向都将不胜感激。

此外,我正在尝试从 mysql 数据库中隐藏 ID,而是显示本地显示的序列号,是否有可能实现这一点,如果可以,如何实现?

非常感谢。

最佳答案

第 1 步.将您的数据库连接代码放入 database_connection.php

$servername = "remotedbIP";
$username = "root";
$password = "Password";
$dbname = "installation";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

第 2 步。将连接包含在索引页的标题中

include("database_connection.php");

第 3 步。在表 tbody 中添加这些 php 代码并删除以前的代码

<?php
$sql = "SELECT id, date, ip, activate FROM software";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
         echo "<tr>";
         echo "<td> $row['id'] </td>";
         echo "<td> $row['date'] </td>";
         echo "<td> $row['ip'] </td>";
         echo "<td> $row['activate'] </td>";
         echo "</tr>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

关于php - 使用 PHP/Jquery 显示从 Mysql 数据库到 Html5 bootstrap 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829813/

相关文章:

mysql - 如何解决错误: ERROR 1442 (HY000) Can't update table 'table name' in stored function/trigger?

php - MYSQLI new query within a while fetch

php - 如何在语句执行期间保持临时 mysqli 表在 php 中可用?

php - 如何将mysql改为mysqli?

php - 安装 ruby​​ 会降低我的 vps 速度吗?

php - 重新加载 php 脚本时,输入文本字段中空格字符后的文本被截断

php - jQuery AJAX POST 到 mysql 表以获取动态表数据-我是否以正确的方式执行此操作?

php - PHP生成的Word文档的显示方式

php - 文件名的字符串清理程序

PHP 合并两个表并列出第一个表中的所有内容