从 sql 表创建 HTML 表


Create HTML table from sql table

>我在 SQL 数据库中有一个表,其中包含以下字段:ID、姓名、电子邮件、大学、语言和经验。我想创建一个从SQL获取数据并输出最后10个结果的html表?我该怎么做?

如果这是一个非常简单的问题,我很抱歉,我对PHP和SQL知之甚少。

这是我现在拥有的代码,它只显示名称而不是在表中:

    <html>
    <head>
        <title>Last 5 Results</title>
    </head>
    <body>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo");
            while($row = mysql_fetch_array($results)) {
                echo $row['Name'] . "</br>";

            ?>
    </body>
</html>

这里有一些东西可以帮助你创建表格并获得更多的php和mysql知识。

此外,还应将连接逻辑和查询移动到流程的开头,从而避免在加载页面时出错,并显示比mysql_error更准确的错误。

编辑:如果您的ID递增,那么您可以添加ORDER BY子句,
更改: SELECT * FROM demo LIMIT 10至: SELECT * FROM demo LIMIT 10 ORDER BY id

<html>
    <head>
        <title>Last 10 Results</title>
    </head>
    <body>
        <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Name</td>
            </tr>
        </thead>
        <tbody>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo LIMIT 10");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['Id']?></td>
                    <td><?php echo $row['Name']?></td>
                </tr>
            <?php
            }
            ?>
            </tbody>
            </table>
    </body>
</html>

我非常恼火地一遍又一遍地将相同的代码粘贴在一起以从SQL查询构建HTML表。

因此,在这里我们使用一个函数,该函数获取mysqli结果并将它们一起粘贴到一个普通的简单 HTML 表中,该表根据数据库中的列名具有列名:

function sql_to_html_table($sqlresult, $delim="'n") {
  // starting table
  $htmltable =  "<table>" . $delim ;   
  $counter   = 0 ;
  // putting in lines
  while( $row = $sqlresult->fetch_assoc()  ){
    if ( $counter===0 ) {
      // table header
      $htmltable .=   "<tr>"  . $delim;
      foreach ($row as $key => $value ) {
          $htmltable .=   "<th>" . $key . "</th>"  . $delim ;
      }
      $htmltable .=   "</tr>"  . $delim ; 
      $counter = 22;
    } 
      // table body
      $htmltable .=   "<tr>"  . $delim ;
      foreach ($row as $key => $value ) {
          $htmltable .=   "<td>" . $value . "</td>"  . $delim ;
      }
      $htmltable .=   "</tr>"   . $delim ;
  }
  // closing table
  $htmltable .=   "</table>"   . $delim ; 
  // return
  return( $htmltable ) ; 
}

示例用法:

$DB = new mysqli("host", "username", "password", "database");
$sqlresult = $DB->query( "SELECT * FROM testtable LIMIT 1 ;" ) ; 
echo sql_to_html_table( $sqlresult, $delim="'n" ) ; 

对查询结果调用table( $result );将生成一个基于 html 的表,而不管您提供给它的 sql 数组的大小如何。 我希望这对:)有所帮助

<?php
function table( $result ) {
    $result->fetch_array( MYSQLI_ASSOC );
    echo '<table>';
    tableHead( $result );
    tableBody( $result );
    echo '</table>';
}
function tableHead( $result ) {
    echo '<thead>';
    foreach ( $result as $x ) {
    echo '<tr>';
    foreach ( $x as $k => $y ) {
        echo '<th>' . ucfirst( $k ) . '</th>';
    }
    echo '</tr>';
    break;
    }
    echo '</thead>';
}
function tableBody( $result ) {
    echo '<tbody>';
    foreach ( $result as $x ) {
    echo '<tr>';
    foreach ( $x as $y ) {
        echo '<td>' . $y . '</td>';
    }
    echo '</tr>';
    }
    echo '</tbody>';
}
您可能

有兴趣使用mysql_fetch_assoc()获取(以便获取关联数组中的数据:keys=>value)。键中是您的列名;因此,对于每一行,您可以遍历每一列(带 array_keys() )并打印其值。

$results = mysql_query("SELECT * FROM demo");
while($row = mysql_fetch_assoc($results)) {
    foreach (array_keys($row) as $column) {
        echo $row[$key] . "</br>";
    }
}

(之后,您可以将array_keys($row)缓存在仅设置一次的变量中,因为它的值在运行结果时不会更改。

尽管已经有一段时间了,我还是要把我的 2 美分投入其中:使用函数(甚至更好 - 类)。 从 mysql 结果创建表是一项非常常见的任务。

<!DOCTYPE html>
<html>
    <head><title>Create Tables from MySQL using functions</title></head>
<body>
<?php
db_connect();
// assuming you have an auto increment id as the first column
$result = mysql_query("SELECT * FROM demo ORDER BY 1 DESC LIMIT 10");
print createTable(array_result($result));
?>
</body>
</html>
<?php 
/**
 * Quick mysql result function
 *
 * @param $result
 * @return array
 */
function array_result($result)
{
    $args = array();
    while ($row = mysql_fetch_assoc($result)) {
        $args[] = $row;
    }
    return $args;
}

/**
 * Connect to db
 * 
 * @param string $db_host
 * @param string $db
 */
function db_connect($db_host = "localhost", $db = "apploymentdevs")
{
    $connect = mysql_connect($db_host,"root", "root");
    if (!$connect) {
        die(mysql_error());
    }
    mysql_select_db($db);
}
/**
 * Create a table from a result set
 *
 * @param array $results
 * @return string
 */
function createTable(array $results = array())
{
    if (empty($results)) {
        return '<table><tr><td>Empty Result Set</td></tr></table>';
    }
    // dynamically create the header information from the keys
    // of the result array from mysql
    $table = '<table>';
    $keys = array_keys(reset($results));
    $table.='<thead><tr>';
    foreach ($keys as $key) {
        $table.='<th>'.$key.'</th>';
    }
    $table.='</tr></thead>';
    // populate the main table body
    $table.='<tbody>';
    foreach ($results as $result) {
        $table.='<tr>';
        foreach ($result as $val) {
            $table.='<td>'.$val.'</td>';
        }
        $table.='</tr>';
    }
    $table.='</tbody></table>';
    return $table;
}

我希望你知道如何使用 HTML 制作表格,并<table>, <tr> and <td>.

在while循环中修复一个表,并将"SELECT * FROM demo LIMIT 10"用作SQL查询。