0

我刚刚编写了简单的 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 表格

4

3 回答 3

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_

于 2013-11-05T19:44:37.987 回答
0

这是我编写的一个函数,用于将 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;
}

希望它会帮助你。

于 2013-11-05T19:40:41.730 回答
0

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);
于 2013-11-05T20:44:40.730 回答