使用 PHP & HTML 页面显示来自 MySQL 数据库的数据
Display data from MySQL database using PHP & HTML Page
我是 PHP 的初学者。我想 Select 并使用 PHP 和 HTML 页面显示来自 MySQL 数据库的数据。
我希望 PHP 和 HTML 文件分开,因为我将使用 jQuery Mobile 和 Phonegap,而 Phonegap 不支持 PHP 文件,所以我必须将 PHP 文件放在网络服务器中。
我已经尝试寻找教程,但实际上互联网上的每个教程都在一个 PHP 页面中展示了如何完成所有操作。
这是我的代码,它工作正常,但它在单个 .PHP 文件中:
<html>
<head>
<title> Display Data </title>
</head>
<body>
<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
</tr>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','12345');
//Select Database
if(!mysqli_select_db($con,'profiles'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM users";
//Execute the SQL query
$records = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Email']."</td>";
}
?>
</table>
</body>
</html>
提前感谢您的宝贵时间。
您可以按照这些思路进行尝试。请记住,getemployees.php 文件必须以一种格式生成输出,该格式可以直接注入 div 以在您的情况下看起来像 table。否则,您可以依赖 JSON 响应,然后循环遍历 JSON 数据记录并在客户端生成输出。
HTML 文件:
<html>
<head>
<title> Display Data </title>
<script>
function getEmployees() {
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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getemployees.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtHint"></div>
<script>getEmployees();</script>
</body>
</html>
PHP 文件:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','12345');
//Select Database
if(!mysqli_select_db($con,'profiles'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM users";
//Execute the SQL query
$records = mysqli_query($con,$sql);
echo "<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
</tr>";
while($row = mysqli_fetch_array($records)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
我是 PHP 的初学者。我想 Select 并使用 PHP 和 HTML 页面显示来自 MySQL 数据库的数据。
我希望 PHP 和 HTML 文件分开,因为我将使用 jQuery Mobile 和 Phonegap,而 Phonegap 不支持 PHP 文件,所以我必须将 PHP 文件放在网络服务器中。
我已经尝试寻找教程,但实际上互联网上的每个教程都在一个 PHP 页面中展示了如何完成所有操作。
这是我的代码,它工作正常,但它在单个 .PHP 文件中:
<html>
<head>
<title> Display Data </title>
</head>
<body>
<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
</tr>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','12345');
//Select Database
if(!mysqli_select_db($con,'profiles'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM users";
//Execute the SQL query
$records = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Email']."</td>";
}
?>
</table>
</body>
</html>
提前感谢您的宝贵时间。
您可以按照这些思路进行尝试。请记住,getemployees.php 文件必须以一种格式生成输出,该格式可以直接注入 div 以在您的情况下看起来像 table。否则,您可以依赖 JSON 响应,然后循环遍历 JSON 数据记录并在客户端生成输出。
HTML 文件:
<html>
<head>
<title> Display Data </title>
<script>
function getEmployees() {
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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getemployees.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="txtHint"></div>
<script>getEmployees();</script>
</body>
</html>
PHP 文件:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','12345');
//Select Database
if(!mysqli_select_db($con,'profiles'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM users";
//Execute the SQL query
$records = mysqli_query($con,$sql);
echo "<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Email </th>
</tr>";
while($row = mysqli_fetch_array($records)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>