使用php从数据库访问数据并将数据输出到HTML

时间:2017-03-20 00:21:46

标签: php html mysql

我正在创建一个博客,我需要创建一个从数据库中检索数据的php文件(我已经完成了)。这个php文件然后将数据输出到html(我需要帮助的地方)。 php和html必须是单独的文件。

这两个文件是:   - index.php:重定向到index.html以显示博客条目。   - index.html:显示存储在条目文件中的博客条目,如果没有条目,则将用户重定向到login.html。

php文件上的echo输出index.php上数据库中的数据,这是我不想要的。我想以某种方式将数据库中的数据输出到index.html。

我是编码的新手,所以请原谅糟糕的编码。

提前致谢。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "addentry";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, title, content, date FROM posts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         header('location: index.html');   

        echo " " . $row["date"]. "<br>"; 
        echo " " . $row["title"]."<br>";
        echo " ". $row["content"];
        }
    } 
else {
    echo "0 results";
    }
$conn->close();
?>
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- image.html
     A trivial document
     -->
<html xmlns = "http://www.w3.org/1999/xhtml">


    <head><title> My Blog </title>

    <style type ="text/css">

        body{ 

        position: fixed; 
        overflow:overlay;
        width: 100%;
        top: -20px;
        left: -20px;
        right: -40px;
        bottom: -40px;       


        height: auto;
        background-image:url(image.jpg);
        background-size: cover;



        }
        .container{

            background-color: #ecdad6;          
            padding: 30px;
            width:920px;        
            margin-left: 25%;
            padding-bottom:1000px;
            padding-left:0px;
            border: 2px solid black;
            }

        .links{
            position: absolute;

            padding-right: 135px;
            padding-bottom: 800px;              
            margin-left: 680px;
            margin-right: 100px;
            font-size: 20px;
            word-wrap: break-word;
            top:-3px;


        }


        .blog{
            position: absolute;
            width:678px;
            padding-bottom: 920px;              
            margin-left: 10px;
            font-size: 20px;
            text-align: left;            
            word-wrap: break-word;

            }
        ul li { margin-top: -10px; }

        }

    } 
    </style>
    <body>

        <!--Logo & hyperlinked -->
        <p align = "center"><a href="index.html"><img src = "Logo.jpg"      alt="My logo" width="10%" height="10%"/></a></p> 
        <br/>
        <hr width="50%">

        <div class="container">                         
            <div class="blog"> 
                <form action='index.php' method='get'></form>
                <div class="links">
                    <a href="index.html"> <ul><li>home</li></ul></a> 
                    <a href="login.html"> <ul><li>logIn</li></ul></a> 
                    <a href="entry.html"> <ul><li>add_entry</li></ul></a>                 
                </div>                  
            </div>     

        </div>

    </body>
</html>

<!-- end snippet -->

1 个答案:

答案 0 :(得分:0)

您需要php来访问数据才能在页面上显示结果。 HTML不是一种动态语言,也不能在它自己的语言上做到这一点。 你可以实现你所要求的一种方法是让php解析器解释.html文件,虽然我不确定你真正想要的是什么,但为了以防万一,请参阅以下链接: / p>

Using .htaccess to make all .html pages to run as .php files?

这假设您使用apache作为Web服务器。 IIS具有类似的功能。请查看您的Web服务器文档以获取更多详细信息。

另一个选择是使用AJAX(javascript)和远程php脚本将动态内容加载到页面中。看看http://www.w3resource.com/ajax/working-with-PHP-and-MySQL.php

您可能还想看看:https://www.w3schools.com/howto/howto_html_include.asp 该页面上可能还有一些内容可以帮助您。

虽然您还没有说明为什么需要从index.php重定向到index.html,但我强烈建议您只使用index.php或index.html运行。

两者都没有必要,可以通过帖子中的信息来确定。

您可以使用php打印所需的所有html。

请参阅以下示例:

创建header.php

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- image.html
     A trivial document
     -->
<html xmlns = "http://www.w3.org/1999/xhtml">


    <head><title> My Blog </title>

    <style type ="text/css">

        body{ 

        position: fixed; 
        overflow:overlay;
        width: 100%;
        top: -20px;
        left: -20px;
        right: -40px;
        bottom: -40px;       


        height: auto;
        background-image:url(image.jpg);
        background-size: cover;



        }
        .container{

            background-color: #ecdad6;          
            padding: 30px;
            width:920px;        
            margin-left: 25%;
            padding-bottom:1000px;
            padding-left:0px;
            border: 2px solid black;
            }

        .links{
            position: absolute;

            padding-right: 135px;
            padding-bottom: 800px;              
            margin-left: 680px;
            margin-right: 100px;
            font-size: 20px;
            word-wrap: break-word;
            top:-3px;


        }


        .blog{
            position: absolute;
            width:678px;
            padding-bottom: 920px;              
            margin-left: 10px;
            font-size: 20px;
            text-align: left;            
            word-wrap: break-word;

            }
        ul li { margin-top: -10px; }

        }

    } 
    </style>
    <body>

        <!--Logo & hyperlinked -->
        <p align = "center"><a href="index.html"><img src = "Logo.jpg"      alt="My logo" width="10%" height="10%"/></a></p> 
        <br/>
        <hr width="50%">

        <div class="container">                         
            <div class="blog"> 
                <form action='index.php' method='get'></form>
                <div class="links">
                    <a href="index.html"> <ul><li>home</li></ul></a> 
                    <a href="login.html"> <ul><li>logIn</li></ul></a> 
                    <a href="entry.html"> <ul><li>add_entry</li></ul></a>                 
                </div>                  
            </div> 

创建footer.php

        </div>

    </body>
</html>

<!-- end snippet -->

修改index.php文件,如下所示:

<?php

include("header.php");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "addentry";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, title, content, date FROM posts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         header('location: index.html');   

        echo " " . $row["date"]. "<br>"; 
        echo " " . $row["title"]."<br>";
        echo " ". $row["content"];
        }
    } 
else {
    echo "0 results";
    }
$conn->close();

include("footer.php);
?>

现在你只需要调用index.php文件,html页眉和页脚就会包含在php文件中。