使用 PHP 从数据库中获取数据并在 HTML 表中显示数据
本教程将逐步教你如何使用 PHP 获取 MySQL 表并在 HTML 中显示记录。
在 MySQL 中创建数据库和表
首先,我们将创建一个 "demo"
数据库和一个 "products"
表。你可以使用 PHPMyAdmin MySQL
或 SQLyog
执行以下 SQL 查询:
MySQL 查询:
/*Your SQL queries*/CREATEDATABASEdemo;/*phpmyadmin MySQL Database Query*//*or*/CREATEDATABASEdemo;/*SQLyog Database Query*/USEdemo;/*Table structure*/DROPTABLEIFEXISTS`products`;CREATETABLE`products`(`id`int(11)NOTNULL,`Manufacturer`char(60)DEFAULTNULL,`Module`char(60)DEFAULTNULL,`Series`char(60)DEFAULTNULL,`MPN`char(60)DEFAULTNULL,`Function`char(60)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4;/*Data for the table*/insertinto`products`(`id`,`Manufacturer`,`Module`,`Series`,`MPN`,`Function`)values(1,'Microsoft','Operation System','A','1263187','OS'),(2,'Amazon','Web Services','B','3473747','Web'),(3,'Rockwell Automation','Electronic Modules','C','9854747','Machine Control'),(4,'Facebook','Social Connectivity','D','1271517','Social'),(5,'Google','Search Engine','E','6372673','Search');
要导入这些记录,你可以复制此查询并直接在 PHPMyAdmin MySQL
或 SQLyog
中运行。
表格 "products"
包含 5 个表格行和列,如下所示:
- 产品编号
- 产品制造商
- 模块类型
- 产品系列
- 产品功能
创建数据库后,我们将用 PHP 将我们的 database.php
文件与 MySQL 服务器连接起来。
在 PHP 中连接到 MySQL 服务器
让我们了解 PHP 中使用的几个关键 MySQL 函数。
-
define()
– 定义本地主机信息。 -
mysqli_connect()
– 通过从define()
函数传递参数连接到 MySQL。 -
die(mysqli_connect_error())
– 在发生数据库故障和db
时显示错误。
代码片段(database.php
):
<?php
define("server", "localhost");
define("user", "root");
define("password", "");
define("database", "demo");
//mysql_connect(); parameters
$connect = mysqli_connect(server, user, password, database);
//run a simple condition to check your connection
if (!$connect)
{
die("You DB connection has been failed!: " . mysqli_connect_error());
}
$connection = "You have successfully connected to the mysql database";
//echo $connection;
?>
输出:
You have successfully connected to the MySQL database.
现在我们已连接到 MySQL 服务器,让我们检索 PHP 脚本中的数据。
使用 PHP 在 HTML 表格中显示数据
我们将使用 require_once()
函数包含 database.php
。然后一个 while
循环将从 mysql_fetch_array()
属性动态创建数据。
HTML (Index.php
):
<!DOCTYPE html>
<body>
<head>
<title> Fetch data from the database in show it into a HTML table dynamically</title>
<link rel="stylesheet" href="style.css">
</head>
<form action="index.php" method="post" align="center">
<input type="submit" name="fetch" value="FETCH DATA" />
</form>
样式 style.css
和 HTML
仅用于我们在 index.php
文件中合并的前端问题。
PHP 脚本 (Index.php
):
<?php
//fetch connection details from database.php file using require_once(); function
require_once ('database.php');
//check if it work!
echo $connection; //from database.php file
if (isset($_POST['fetch']))
{
//mysql_query() performs a single query to the currently active database on the server that is associated with the specified link identifier
$response = mysqli_query($connect, 'SELECT * FROM products');
echo "<table border='2' align='center'>
<H2 align='center'> Products Table </h2>
<tr>
<th>Product ID</th>
<th>Product Manufacturer</th>
<th>Product Type</th>
<th>Product Series</th>
<th>MPN</th>
<th>Product Function</th>
</tr>";
while ($fetch = mysqli_fetch_array($response))
{
echo "<tr>";
echo "<td>" . $fetch['id'] . "</td>";
echo "<td>" . $fetch['Manufacturer'] . "</td>";
echo "<td>" . $fetch['Module'] . "</td>";
echo "<td>" . $fetch['Series'] . "</td>";
echo "<td>" . $fetch['MPN'] . "</td>";
echo "<td>" . $fetch['Function'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
}
?>
isset($_POST['fetch'])
函数在表单提交时触发。然后我们使用 mysql_query('Your query')
从 products
表中选择所有记录。
我们将它们存储在 $response
变量中。之后,我们使用 while
循环生成一个表,直到 mysql_fetch_array()
完成以数组索引的形式获取记录。
$fetch['array_index']
以 mysql_query
成功影响的先前存储的数组索引为目标。
输出:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。