<legend id='DhIjT'><style id='DhIjT'><dir id='DhIjT'><q id='DhIjT'></q></dir></style></legend>
    <i id='DhIjT'><tr id='DhIjT'><dt id='DhIjT'><q id='DhIjT'><span id='DhIjT'><b id='DhIjT'><form id='DhIjT'><ins id='DhIjT'></ins><ul id='DhIjT'></ul><sub id='DhIjT'></sub></form><legend id='DhIjT'></legend><bdo id='DhIjT'><pre id='DhIjT'><center id='DhIjT'></center></pre></bdo></b><th id='DhIjT'></th></span></q></dt></tr></i><div id='DhIjT'><tfoot id='DhIjT'></tfoot><dl id='DhIjT'><fieldset id='DhIjT'></fieldset></dl></div>

        <bdo id='DhIjT'></bdo><ul id='DhIjT'></ul>
    1. <tfoot id='DhIjT'></tfoot>

      1. <small id='DhIjT'></small><noframes id='DhIjT'>

      2. 使用 PHP 在 HTML 表格中显示 MySQL 结果

        时间:2023-07-30
              <tbody id='joRCW'></tbody>

            <tfoot id='joRCW'></tfoot>
              • <legend id='joRCW'><style id='joRCW'><dir id='joRCW'><q id='joRCW'></q></dir></style></legend>

                <small id='joRCW'></small><noframes id='joRCW'>

              • <i id='joRCW'><tr id='joRCW'><dt id='joRCW'><q id='joRCW'><span id='joRCW'><b id='joRCW'><form id='joRCW'><ins id='joRCW'></ins><ul id='joRCW'></ul><sub id='joRCW'></sub></form><legend id='joRCW'></legend><bdo id='joRCW'><pre id='joRCW'><center id='joRCW'></center></pre></bdo></b><th id='joRCW'></th></span></q></dt></tr></i><div id='joRCW'><tfoot id='joRCW'></tfoot><dl id='joRCW'><fieldset id='joRCW'></fieldset></dl></div>
                  <bdo id='joRCW'></bdo><ul id='joRCW'></ul>
                • 本文介绍了使用 PHP 在 HTML 表格中显示 MySQL 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  CodingBiz 更新:

                  我把它放在我的代码中:

                  I'm putting this in my code:

                  for($i=1;$i<=$numRows;$i++) {
                      $output .= '<tr>';
                      $row = $this->fetchAssoc($result);
                      $colRow = $this->fetchAssoc($colResult);
                      foreach($colRow as $colName) {
                          $output .= "<td>".$row[$colName]."</td>";
                      }
                      $output .= '</tr>';
                  }
                  

                  代替

                  for($i=1;$i<=$numRows;$i++) {
                      $output .= '<tr>';
                      $row = $this->fetchAssoc($result);
                      for($j=1;$j<=$colNumRows;$j++) {
                          $colRow = $this->fetchAssoc($colResult);
                          $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                      }
                      $output .= '</tr>';
                  }
                  

                  这有什么问题吗?

                  原帖:

                  我正在 PHP 类中编写一个函数来在表中显示查询结果.我没有自己构建任何表格,我希望它使用 PHP 完成所有工作.到目前为止,这是我的代码:

                  I'm writing a function in a PHP class to display the results of a query in a table. I'm not structuring any of the table myself, I want it everything to be done using PHP. Here is my code so far:

                  function allResults($table,$cols) {
                      if(isset($cols)) {
                          $query = "SELECT $cols FROM $table";
                      }
                      else {
                          $query = "SELECT * FROM $table";
                      }
                      $result = $this->query($query);
                      $numRows =  $this->numRows($result);
                      $colQuery ="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='shareride'  AND TABLE_NAME='$table'";
                      $colResult = $this->query($colQuery);
                      $colNumRows = $this->numRows($colResult);
                  
                      $output = '<table class="allResults">';
                      $output .= '<tr>';
                      for($i=1;$i<=$colNumRows;$i++) {
                          $colRow = $this->fetchAssoc($colResult);
                          $output .= "<td>".$colRow["COLUMN_NAME"]."</td>";
                      }
                      $output .= '</tr>';
                      for($i=1;$i<=$numRows;$i++) {
                          $output .= '<tr>';
                          $row = $this->fetchAssoc($result);
                          for($j=1;$j<=$colNumRows;$j++) {
                              $colRow = $this->fetchAssoc($colResult);
                              $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                          }
                          $output .= '</tr>';
                      }
                      $output .= '</table>';
                      return $output;
                  }
                  

                  如果不清楚,query指的是mysqli_querynumRows指的是mysqli_num_rows>fetchAssoc 指的是 mysqli_fetch_assoc.数据库名称是shareride".

                  In case it is unclear, query refers to mysqli_query, numRows refers to mysqli_num_rows, and fetchAssoc refers to mysqli_fetch_assoc. The database name is "shareride."

                  我知道我在这一行中遗漏了一些东西:

                  I know I am missing something in this line:

                  $output .= "<td>".$row[$colRow["COLUMN_NAME"]]."</td>";
                  

                  但我只是不知道它是什么.现在,我正确显示了所有表格列标题,并获得了正确数量的内容行,但我无法使用数据库中的实际数据填充这些行.

                  but I just don't know what it is. Right now, I get all the table column titles displayed correctly, and I get the correct number of content rows, but I just can't populate those rows with the actual data from the database.

                  我错过了什么?任何帮助将非常感激!

                  What am I missing? Any help would be GREATLY appreciated!

                  推荐答案

                  从同一个结果集中获取数据和列名

                  Get the data and column names from the same result set

                    <?php
                    $i = 0;
                    $colNames = array();
                    $data = array();
                    while($row = ***_fetch_assoc($res)) //where $res is from the main query result not schema information
                    {
                       //get the column names into an array $colNames
                       if($i == 0) //make sure this is done once
                       {
                          foreach($row as $colname => $val)
                             $colNames[] = $colname;
                       }
                  
                       //get the data into an array
                       $data[] = $row;
                  
                       $i++;
                    }
                  
                   ?>
                  

                  更新:@YourCommonSense 建议替换上面的代码,它有效,简单且更短 - 一种无需像我那样循环即可获取列名/数组键的方法

                    $data = array();
                    while($row = mysql_fetch_assoc($res))
                    {
                       $data[] = $row;
                    }
                  
                    $colNames = array_keys(reset($data))
                  

                  继续:打印表格

                   <table border="1">
                   <tr>
                      <?php
                         //print the header
                         foreach($colNames as $colName)
                         {
                            echo "<th>$colName</th>";
                         }
                      ?>
                   </tr>
                  
                      <?php
                         //print the rows
                         foreach($data as $row)
                         {
                            echo "<tr>";
                            foreach($colNames as $colName)
                            {
                               echo "<td>".$row[$colName]."</td>";
                            }
                            echo "</tr>";
                         }
                      ?>
                   </table>
                  

                  测试结果

                  您可以看到我如何将数据检索与表生成分离.它们现在相互依赖,您可以通过使用静态数据填充数组来测试没有数据库的表生成

                  You can see how I separated the data retrieval from table generation. They are dependent of each other now and you can test your table generation without the database by populating the arrays with static data

                  你也可以把它们做成单独的函数.

                  You can also make them into separate functions.

                  这篇关于使用 PHP 在 HTML 表格中显示 MySQL 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:无法弄清楚如何运行 mysqli_multi_query 并使用上次查 下一篇:如何确保我从 MySQLi::multi_query 中捕获到所有错误

                  相关文章

                  最新文章

                  <i id='iZJo8'><tr id='iZJo8'><dt id='iZJo8'><q id='iZJo8'><span id='iZJo8'><b id='iZJo8'><form id='iZJo8'><ins id='iZJo8'></ins><ul id='iZJo8'></ul><sub id='iZJo8'></sub></form><legend id='iZJo8'></legend><bdo id='iZJo8'><pre id='iZJo8'><center id='iZJo8'></center></pre></bdo></b><th id='iZJo8'></th></span></q></dt></tr></i><div id='iZJo8'><tfoot id='iZJo8'></tfoot><dl id='iZJo8'><fieldset id='iZJo8'></fieldset></dl></div>
                  1. <small id='iZJo8'></small><noframes id='iZJo8'>

                  2. <tfoot id='iZJo8'></tfoot>
                    1. <legend id='iZJo8'><style id='iZJo8'><dir id='iZJo8'><q id='iZJo8'></q></dir></style></legend>
                        <bdo id='iZJo8'></bdo><ul id='iZJo8'></ul>