我设法从我的网站数据库中获取信息,但我不知道如何在表格中显示结果以及如何设置样式。我尝试将<table>
放在整个PHP之外,显然不起作用。我尝试在结果回显之前回显<table>
标记,然后在它之后回显</table>
标记,但是没有这样做。这是我正在使用的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<span style='color:white;'>"."<br> Name: ".$row["name"]."<br> Description: ".$row["description"]."<br> Content: ".$row["content"] ."<br>"."</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我仍然是PHP的新手,试图了解整个过程是如何工作的。提前谢谢!
答案 0 :(得分:0)
试试这个:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<table>";
echo "<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>";
echo "<tbody>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["description"]."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
答案 1 :(得分:0)
尝试这种方法:
[... some HTML header and code ...]
<table>
<thead>
<tr><th>Name</th><th>Description</th><th>Content</th></tr>
</thead>
<tbody>
<?php
[ ... extract something from the database ...]
while($row = $result->fetch_assoc())
{
echo "<tr><td>$row[name]</td><td>$row[description]</td><td>Content</td></tr>\n";
}
}
?>
</tbody>
</table>
[... some HTML footer ...]
显然,用您自己的代码替换[...]
部分。我们的想法是使用PHP输出HTML代码的动态部分,并且是PHP的 power 和HTML结合使用。
我尽量避免在我的PHP中“回显”很多HTML,但它确实有用(参考其他答案)。
详细信息:设置<table>
时,您应设置标题部分<thead>
<tfoot>
(如果适用)和正文部分<tbody>
。参考What is the purpose for HTML's tbody?
答案 2 :(得分:0)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<table>';
echo '<tr><td>Name</td><td>Description</td><td>Content</td></tr>'
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["description"]."</td><td>".$row["content"] ."</td></tr>";
}
echo '</table>';
} else {
echo "0 results";
}
$conn->close();
&GT;
答案 3 :(得分:0)
基本表格格式为
例如:
<table>
<tr>
<td>row 1 item 1</td>
<td>row 1 item 2</td>
</tr>
<tr>
<td>row 2 item 1</td>
<td>row 2 item 2</td>
</tr>
</table>
所以试试:
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["description"]."</td>";
echo "<td>".$row["content"]."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
答案 4 :(得分:0)
<?php
function tableV1 ($row) {
echo '<tr>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '</tr>';
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
?>
在输出任何内容之前,始终首先执行数据库连接,这样,您可以创建自定义错误消息,而不是显示失败的数据库连接或根本没有内容。
<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>
在<head></head>
内使用样式来设置表格的样式,称为CSS。
<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</thead>
<tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
tableV1($row);
}
} else {
echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
</tbody>
</table>
从数据库输出内容。
<?php
$conn->close();
?>
最后关闭数据库连接。一起来:
<?php
function tableV1 ($row) {
echo '<tr>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '</tr>';
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>
</head>
<body>
<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</thead>
<tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
tableV1($row);
}
} else {
echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>
<?php
$conn->close();
?>