在html表格中显示所有MySQL表格数据

在html表格中显示所有MySQL表格数据

本文介绍了在html表格中显示所有MySQL表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在html表中显示整个 mysql 表。到目前为止,我发现下面的代码可以显示字段:

 <?php 
$ con = mysqli_connect ( example.com, Peter 的 ABC123, MY_DB);
//检查连接
if(mysqli_connect_errno()){
echo无法连接到MySQL:。 mysqli_connect_error();
}

$ result = mysqli_query($ con,SELECT * FROM Persons);

echo< table border ='1'>
< tr>
< th>名字< / th>
< th> ; / th>
< / tr>;

while($ row = mysqli_fetch_array($ result)){
echo< tr>;
回显< td> 。 $ row ['FirstName']。 < / TD> 中;
回显< td> 。 $ row ['LastName']。 < / TD> 中;
回声< / tr>;
}
echo< / table>;

mysqli_close($ con);
?>

有没有一种方法可以显示所有列/行而不必将所有列名键入为

echo< td> 。 $ row ['FirstName']。 < / td>;

解决方案

所有列。或者,您可以添加条件过滤器。



然而,看起来您对网络开发并不陌生,所以我以一种简单的方式实现它。

 <?php 
$ con = mysqli_connect(example.com,peter,abc123,my_db);
//检查连接
if(mysqli_connect_errno())
{
echo无法连接到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 ++;
回显< tr>;
foreach($ row as $ key => $ value){
echoth 。 $键。 < /第> 中;
}
echo< / tr>;
}
echo< tr>;
foreach($ row为$ value){
echo< td> 。 $值。 < / TD> 中;
}
echo< / tr>;
}
echo< / table>;

mysqli_close($ con);
?>


I am trying to display a whole mysql table in an html table. So far I have found the below code which works to display fields:

<?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);
?>

Is there a way that I can display all columns/rows without having to type all the column names as

echo "<td>" . $row['FirstName'] . "</td>";

解决方案

You can do it if you are going to show all columns. Or, you can add your filters conditional.

However, as it seems you are new to web development, I make it in a simple way.

<?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);
?>

这篇关于在html表格中显示所有MySQL表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:42