【问题标题】:PHP: MySQL to tablePHP:MySQL 到表
【发布时间】:2013-11-05 19:34:43
【问题描述】:

我刚刚编写了简单的 PHP 代码来打印其中的一些行

我的表结构是这样的:

这是我的 PHP 代码:

$user = mysql_query("SELECT usr FROM ava_members");
$id = mysql_query("SELECT id FROM ava_members");
$email = mysql_query("SELECT email FROM ava_members");
$dt = mysql_query("SELECT dt FROM ava_members");

//$result = mysql_query("SELECT id,email,dt,regIP FROM ava_members ORDER BY dt LIMIT 5");
$final = "";
$final .= "<table border='1'><tr>";


/* TABELA */
$final .= "<td>Nick</td>";
$final .= "<td>ID</td>";
$final .= "<td>Email</td>";
$final .= "<td>Data</td>";
//$final .= "</tr>\n";
/* TABELA */

//user
while($row = mysql_fetch_row($user))
{
    $final .= "<tr>";

    foreach($row as $cell)
        $final .= "<td>$cell <a href=\"\?usrdel=$cell\"\>[DELETE]</a></td>";
        $final .= "</tr>\n";
}

//ID
while($row = mysql_fetch_row($id))
{
   $final .= "<tr>";

    foreach($row as $cell)
        $final .= "<td>$cell</td>";
        $final .= "</tr>\n";
}

//email

while($row = mysql_fetch_row($email))
{
    $final .= "<tr>";

    foreach($row as $cell)
        $final .= "<td>$cell</td>";
        $final .= "</tr>\n";
}

//dt
while($row = mysql_fetch_row($dt))
{
   $final .= "<tr>";

    foreach($row as $cell)
        $final .= "<td>$cell</td>";
        $final .= "</tr>\n";
}

mysql_free_result($user);
mysql_free_result($id);
mysql_free_result($email);
mysql_free_result($dt);

echo '<center>' . $final . '</center>';

还有输出:

但是你可以猜到这不是我想要的...... 我想要的输出应该是这样的:

这很简单 - 只需学习 in2 html 表格

【问题讨论】:

标签: php mysql printing html-table


【解决方案1】:

你应该学习 SQL:

$data = mysql_query("SELECT usr, id, email, dt FROM ava_members");
while ($row = mysql_fetch_row($data))
{
    $final .= "<tr>";

    foreach($row AS $k => $cell) {
        $final .= '<td>' . htmlspecialchars($cell);
        if ($k == 'usr') {
            $final .= '<td><a href="?usrdel=' . $row['id'] . '">[DELETE]</a>';
        }
        $final .= '</td>';
    }
    $final .= "</tr>\n";
}

是的,不要使用mysql_。请改用mysqli_

【讨论】:

    【解决方案2】:

    这是我编写的一个函数,用于将 MySQL 数据库中的数据显示为 HTML 表:

    /* Returns all of the results from a query in one nice table */
    /* Example usage: echo $db->allResults("taxi0", "time, latitude, longitude",40,50); */
    function allResults($table,$cols,$limstart,$lim) {
        if(isset($cols)) {
            $query = "SELECT $cols FROM $table LIMIT $limstart,$lim";
        }
        else {
            $query = "SELECT * FROM $table LIMIT $limstart,$lim";
        }
        $result = $this->query($query);             
        $output = '<table class="allResults">';
        $data = array();
        while($row = $this->fetchAssoc($result)) {
            $data[] = $row;
        }
        $colNames = array_keys(reset($data));
        $output .= "<tr>";
        foreach($colNames as $colName) {
            $output .= "<th>$colName</th>";
        }
        $output .= "</tr>";
        foreach($data as $row) {
            $output .= "<tr>";
            foreach($colNames as $colName) {
                $output .= "<td>".$row[$colName]."</td>";
            }
            $output .= "</tr>";
       }
        $output .= '</table>';
        return $output;
    }
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      PDO 示例,因为它既不被弃用又更有趣:

      // precondition: $row should be a numbered array of column data
      function table_row_from_db_row($row) {
          $result = '';
          foreach($row as $key => $value) {
              $result .= "<td>$value</td>\n";
          }
      
          return "<tr>\n$result</tr>\n";
      }
      
      // precondition: $headers should be an associative array representing the 
      //   first row
      function table_head_from_first_row($row) {
          $result = '';
          foreach($row as $name => $unused) {
              $result .= "<th>$name</th>\n";
          }
      
          return "<tr>\n$result</tr>\n";
      }
      
      // precondition: $sth is a PDO statement handle from a query that returns 
      //   more than 0 rows.
      // postcondition: if there were no rows, returns NULL.  Otherwise returns 
      //   an HTML table as a string.
      function table_from_query_handle($sth) {
          $result = '';
      
          // Fetch the first row so we can get column header names.
          $row = $sth->fetch(PDO::FETCH_ASSOC);
          if(!$row) {
              return NULL;
          }
      
          $result .= table_head_from_first_row($row);
      
          do {
              $result .= table_row_from_db_row($row);
          }
          while($row = $sth->fetch(PDO::FETCH_ASSOC));
      
          return "<table>\n$result</table>\n";
      }
      
      // Alias the column names to the table headers we want to use on the page.
      // If we wanted to build the table manually it would still help that we don't
      // have two different names for each field (e.g. `dt` and `data`).
      $query = "select usr Nick, id ID, email Email, dt Data from ava_members";
      
      // Connect to the database.  This belongs in another file in production code.
      $dbh = new PDO('mysql:host=localhost;dbname=test');
      
      // Execute the query and get a statement handle.
      $sth = $dbh->query($query);
      
      echo table_from_query_handle($sth);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 2013-02-23
        • 1970-01-01
        • 2011-04-17
        相关资源
        最近更新 更多