首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP: MySQL到表

PHP: MySQL到表
EN

Stack Overflow用户
提问于 2013-11-05 19:34:43
回答 3查看 139关注 0票数 0

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

我的表结构是这样的:

这是我的PHP代码:

代码语言:javascript
复制
$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表

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-05 19:44:37

您应该学习SQL:

代码语言:javascript
复制
$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_代替。

票数 1
EN

Stack Overflow用户

发布于 2013-11-05 19:40:41

下面是我编写的一个函数,用于将来自MySQL数据库的数据显示为一个HTML:

代码语言:javascript
复制
/* 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;
}

希望它能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2013-11-05 20:44:40

PDO示例,因为它既不受欢迎,也更有趣:

代码语言:javascript
复制
// 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19797322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档