我试图在html表中显示整个mysql
表。到目前为止,我已经找到了以下代码,用于显示字段:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if(mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
有没有办法可以显示所有列/行,而无需键入所有列名称
echo "<td>" . $row['FirstName'] . "</td>";
答案 0 :(得分:2)
如果您要显示所有列,则可以执行此操作。或者,您可以有条件地添加过滤器。
但是,由于您似乎对Web开发不熟悉,我会以简单的方式进行。
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
答案 1 :(得分:0)
你可以做这样的事情
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . implode("</td><td>",$row) . "</td></tr>";
}
修改强>
为了尝试使用数据库,请将以下代码粘贴到phpfiddle.org工作区(首先在主页中检查MySQLi库)。
<?php
/**
* Mysqli initial code
*
* User permissions of database
* Create, Alter and Index table, Create view, and Select, Insert, Update, Delete table data
*
* @package PhpFiddle
* @link http://phpfiddle.org
* @since 2012
*/
require "util/public_db_info.php";
$short_connect = new mysqli($host_name, $user_name, $pass_word, $database_name, $port);
$sql = "SELECT * FROM `books` WHERE title='The Martian'";
$result = $short_connect->query($sql);
if (($result) && ($result->num_rows > 0))
{
echo "<table>";
//convert query result into an associative array
while ($row = $result->fetch_array())
{
echo "<tr><td>" . implode("</td><td>",$row) . "</td></tr>";
}
echo "</table>";
$result->free();
}
$short_connect->close();
?>
答案 2 :(得分:0)
要获得这样的垂直表:
PHP代码:
$idProduct = $conexion->real_escape_string($_GET['planId']);
$result = $conexion->query("SELECT * FROM `g4s_products` WHERE id_product = '".$idProduct."'");
if ($result->num_rows > 0) {
$html = '';
while ($row = $result->fetch_assoc()) {
$html .= '<div class="ppalDiv">' .
'<p class="subtitleDetails"><b>Main Benefits</b></p>' .
'<p class="textDetails">' . $row["description"] . '</p>' .
'</div>' .
'<div class="ppalDiv">' .
'<p class="subtitleDetails"><b>Benefits Highlights</b></p>';
echo $html;
echo "<table>";
foreach ($row as $key => $value) {
echo "<tr>";
echo "<th>" . $key . ":</th>";
echo "<td>" . $value . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
mysqli_close($conexion);
CSS代码:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
完成了。享受