使用PHP将MySQL表打印到动态HTML表

piwo6bdm  于 2023-10-15  发布在  PHP
关注(0)|答案(3)|浏览(132)

我正在自学PHP,到目前为止,它一直很好。我现在有点卡住了,所以需要Maven的干预。我试图用PHP打印一个MySQL表到一个动态HTML表。我想根据查询提取的字段(列)和行的数量显示列名和行。我已经尝试过遍历mysqli_fetch_row()的行,并在mysqli_fetch_fields()上使用foreach。但是没有输出,也没有任何错误。下面是我的代码的一部分:

if(isset($_POST['submit']))
{
   $search=$_POST['name'];
   $conn=DBconnect();
   $result=SQLquery($conn, $search);

   if ($result->num_rows > 0) 
   {
      echo "<table id='tbl'><tr>";
      $field=$result->fetch_fields();
// output column names  
     foreach ($field as $col)
     {
        echo "<th>".$col->name."</th>";
     }
     echo "</tr>"

// output data of each row
     while($row = $result->fetch_row()) 
     {
        echo "<tr>";

        for ($i=0;$i<=$result->field_count;$i++)
        {
           echo "<td>".$row[$i]."</td>";
        }

        echo "</tr>";
      }
     echo "</table>";

  }

else  
{
 echo "No data found";
}

}
fcy6dtqo

fcy6dtqo1#

mysqli方法有一点不同,下面的代码提供:

function createTable_from_sql_select_query($sql_link, $query) {

    $result=mysqli_query($sql_link, $query);

    if ($result->num_rows > 0) 
    {
        echo "<table id='tbl'><tr>";

        $field = $result->fetch_fields();
        $fields = array();
        $j = 0;
        foreach ($field as $col)
        {
            echo "<th>".$col->name."</th>";
            array_push($fields, array(++$j, $col->name));
        }
        echo "</tr>";

        while($row = $result->fetch_array()) 
        {
            echo "<tr>";
            for ($i=0 ; $i < sizeof($fields) ; $i++)
            {
                $fieldname = $fields[$i][1];
                $filedvalue = $row[$fieldname];

                echo "<td>" . $filedvalue . "</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

    }

}

createTable_from_sql_select_query($conn, 'SELECT * FROM `Transactions`');
jdzmm42g

jdzmm42g2#

您可以使用这个名为Table的类执行下一个示例:

<?php

class Table {

    public function __construct ($name){  
      $this->data = null;
    }

    public function setData($data) {
      $this->data = $data;
    }

    function getCountColumns(){
      return (int) mysqli_num_fields($this->data);
    }

    function getCountRows(){
      return (int) mysqli_num_rows($this->data);
    }

    function render(){
      $columnsCount = $this->getCountColumns();
      $tableData = $this->data;
      $fields =  mysqli_fetch_fields($tableData);
      echo "<table class='table table-hover'>";
        echo "<thead>";
          echo "<tr>";
            foreach ($fields as $field) {
              echo "<th>";
                echo $field->name ;
              echo "</th>\n";
            }
          echo "</tr>\n";
        echo "</thead>\n";
        echo "<tbody>\n";
          while ($row = mysqli_fetch_array($tableData)) {
            echo "<tr>";
            for ($x=0; $x < $columnsCount; $x++) { 
              echo "<td >" . $row[$x] . "</td>\n";
            }
            echo "</tr>\n";
          }
        echo "</tbody>";
      echo "</table>";
    }

}
?>

当你需要它的时候,把它包含在你的HTML中:

<?php 
  include('Table.php');
  $table = new Table('myTable');
  $result=mysqli_query($sql_link, $query);
  $table->setData($result);
?>
<html>
  <body>
    $table->render();
  </body>
</html>
sigwle7e

sigwle7e3#

在你的代码的第16行缺少一个字符串。可能是这个问题的原因。还要检查数据库连接、查询和POST变量是否工作正常。除此之外,代码的其余部分看起来都很好。

相关问题