想知道我的代码的正确语法。它是一个从mySQL数据库中提取数据的php代码。这段代码应该显示为servername,contact,location,vlan,servicerequest输入的最新数据。我的PHP代码不起作用:
<!DOCTYPE html>
<html>
<div id="button"><a href="http://10.90.4.71/cfg2html_outputs/Checklist/php/index.php">Back to Checklist Page</a></div>
<head>
<title>TESTER</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<style>
table, th, td
{
border: 1px solid grey
}
li.a {list-style-position:inside;}
</style>
</head>
<body>
<table>
<h2><b>TESTER</b></h2>
<table width="1900" align="left">
<tr>
<th width="1900" align="left" colspan="3" bgcolor="#D3D3D3"><b>SERVER INFORMATION</b></th>
</tr>
<tr>
<td width="200" align="left">Server name:</td>
<td width="1700" align="left" colspan="2">
<?php
$conn = mysqli_connect("localhost", "username", "password", "test");
if ($conn-> connect_error) {
die("Connection failed:". $conn-> connect_error);
}
$sql = "SELECT servername, contact, location, vlan, servicerequest from info";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo $row["servername"] ."</td></tr><tr><td width="200" align="left">Contact:</td><td width="1700" align="left" colspan="2">". $row["contact"] ."</td></tr><tr><td width="200" align="left">Location:</td><td width="1700" align="left" colspan="2">". $row["location"] ."</td></tr><tr><td width="200" align="left">VLAN:</td><td width="1700" align="left" colspan="2">". $row["vlan"] ."</td></tr><tr><td width="200" align="left">Service Request/Task:</td><td width="1700" align="left" colspan="2">". $row["servicerequest"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 result";
}
$conn-> close();
?>
</table>
</body>
</html>
我将上面的代码连接到数据库,但是以有组织的表格方式连接css和我想要的正确的表行和表维度和表列。下面是工作的PHP代码(我从youtube视频中获取了想法),但它没有以我想要的有组织的方式显示数据,它还显示我不想要的更早的输入数据,我想要它显示servername,contact,location,vlan,servicerequest的最新修改数据。这是php数据库提取正在运行的代码。我想将以下代码更改为上面的代码,并使上面的代码工作。
<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
</head>
<body>
<table>
<tr>
<th>servername</th>
<th>contact</th>
<th>location</th>
<th>vlan</th>
<th>servicerequest</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "username", "password", "test");
if ($conn-> connect_error) {
die("Connection failed:". $conn-> connect_error);
}
$sql = "SELECT servername, contact, location, vlan, servicerequest from info";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo "<tr><td>". $row["servername"] ."</td><td>". $row["contact"] ."</td><td>". $row["location"] ."</td><td>". $row["vlan"] ."</td><td>". $row["servicerequest"] ."</td></tr>";
}
echo "</table>";
}
else {
echo "0 result";
}
$conn-> close();
?>
</table>
</body>
</html>
再一次。我想在表格中仅显示所有5个变量的最新输入数据:servername,contact,location,vlan,servicerequest。和适当的表格fasion。例如:<tr><td>servername</td><td>$servername</td></tr><tr><td>contact</td><td>$contact</td></tr>
等。它应该只显示在MySQL数据库的最后一行输入的数据。