我需要帮助。我是PHP新手,最近几天一直在尝试解决这个问题。我试图将数据库中的数据解析为一个样式化的HTML表,但找不到任何关于此的教程。我做了一个使用PHP创建的表的解析教程。我想使用这个文件中包含的表。如果有人能告诉我怎么做,并解释一下,我会非常高兴。
这是我尝试使用的PHP文件。从教程里我只能找到一个。

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());

// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Navn</th> <th>Årgang</th> <th>NR</th> <th>Navn</th> <th>Navn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['name'];
    echo "</td><td>"; 
    echo $row['age'];
    echo "</td><td>"; 
    echo $row['issue'];
    echo "</td><td>"; 
    echo $row['Description'];
    echo "</td><td>";
    echo $row['quality'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

这是我想使用的样式表:
/* ------------------
   styling for the tables 
   ------------------   */

body
{
    line-height: 1.6em;
}

#hor-zebra
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 60px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
}
#hor-zebra th
{
    font-size: 14px;
    font-weight: normal;
    padding: 10px 8px;
    color: #039;
}
#hor-zebra td
{
    padding: 8px;
    color: #669;
}
#hor-zebra .odd
{
    background: #e8edff; 

这是一个HTML文件,我希望数据库中的数据显示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
<table id="hor-zebra" summary="Datapass">
    <thead>
        <tr>
            <th scope="col">name</th> //Name off table in DB
            <th scope="col">age</th> //Name off table in DB
            <th scope="col">issue</th> //Name off table in DB
            <th scope="col">Description</th> //Name off table in DB
            <th scope="col">quality</th> //Name off table in DB
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

</body>
</html>

在添加代码后,我的PHP会返回结果。唯一的问题是它没有从style.css中显示我的样式表,而且我还得到了一个错误“
注意:未定义的变量:i在第25行的C:\程序文件(x86)\ EasyPHP-5.3.9\www\Tables\Datamodtagelse.php中
在这下面它返回我的输出:(这是php页面。)
name                    age issue   Description                           quality
Anders And & Co.    1949    1   Dette er en beskrivelse af en tegneserie. Very Fine.

当我打开我的html文件时,它一点也不显示。
我将添加我的文件:
Datamodtagelse.php
<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("tegneserier") or die(mysql_error());

    // Get all the data from the "årgang" table
    $result = mysql_query("SELECT * FROM årgang") 
    or die(mysql_error());  

    echo '<table id="hor-zebra" summary="Datapass">
<thead>
    <tr>
        <th scope="col">name</th>
        <th scope="col">age</th>
        <th scope="col">issue</th>
        <th scope="col">Description</th>
        <th scope="col">quality</th>
    </tr>
</thead>
<tbody>';


    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        if( $i++ % 2 == 0 ) {
            $class = " class='odd'";
        } else {
            $class = "";
        }
        // Print out the contents of each row into a table
        echo "<tr" . $class . "><td>"; 
        echo $row['name'];
        echo "</td><td>"; 
        echo $row['age'];
        echo "</td><td>"; 
        echo $row['issue'];
        echo "</td><td>"; 
        echo $row['Description'];
        echo "</td><td>";
        echo $row['quality'];
        echo "</td></tr>"; 
    } 

    echo "</tbody></table>";
?>

Showcomic.html:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>

样式.css
body
{
    line-height: 1.6em;
}

#hor-zebra
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 60px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
}
#hor-zebra th
{
    font-size: 14px;
    font-weight: normal;
    padding: 10px 8px;
    color: #039;
}
#hor-zebra td
{
    padding: 8px;
    color: #669;
}
#hor-zebra .odd
{
    background: #e8edff; 
}

我的数据库名是:tegneserier
我在数据库中的表是:rgang
我在表中的属性是:
id int(11)自动递增
名称varchar(255)utf8_danish_ci
年龄内景(11)
发布int(11)
说明文字utf8 U丹麦语
在查看代码时,我认为问题在于HTML文件或从.php文件导入样式表和数据?
.php文件和.css文件以及.html文件位于同一文件夹中。
欢迎任何帮助。
很抱歉,这可能只是初学者容易犯的错误。(我们都需要从某个地方开始。)

最佳答案

试试这样的:

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("tegneserier") or die(mysql_error());

    // Get all the data from the "årgang" table
    $result = mysql_query("SELECT * FROM årgang") 
    or die(mysql_error());  

    echo '<table id="hor-zebra" summary="Datapass">
<thead>
    <tr>
        <th scope="col">name</th> //Name off table in DB
        <th scope="col">age</th> //Name off table in DB
        <th scope="col">issue</th> //Name off table in DB
        <th scope="col">Description</th> //Name off table in DB
        <th scope="col">quality</th> //Name off table in DB
    </tr>
</thead>
<tbody>';


    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        if( $i % 2 == 0 ) {
            $class = " class='odd'";
        } else {
            $class = "";
        }
        // Print out the contents of each row into a table
        echo "<tr" . $class . "><td>"; 
        echo $row['name'];
        echo "</td><td>"; 
        echo $row['age'];
        echo "</td><td>"; 
        echo $row['issue'];
        echo "</td><td>"; 
        echo $row['Description'];
        echo "</td><td>";
        echo $row['quality'];
        echo "</td></tr>"; 
    } 

    echo "</tbody></table>";
?>

在HTML文件中:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>

08-04 14:49