用PHP编写html文件

用PHP编写html文件,php,html,Php,Html,我想用PHP在html文件中编写,但只在最后一次编写: 请帮助我: <?php $link = mysql_connect('my_server', 'user', 'password') or die("Can't"); $db = mysql_select_db('database') or die ("Can't select"); $sql = 'SELECT * FROM News LIMIT 3'; $retval = mysql_query($sql,$li

我想用PHP在html文件中编写,但只在最后一次编写: 请帮助我:

<?php 
$link = mysql_connect('my_server', 'user', 'password')
    or die("Can't");
$db = mysql_select_db('database')
    or die ("Can't select");
$sql = 'SELECT * FROM News LIMIT 3';
$retval = mysql_query($sql,$link);

while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
        //echo "<pre>"; print_r($row); echo "</pre>";
        $content = '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
        <div class="block">';
            $content .='<a href="="">'.$row['Title'].'</a>';
            $content .='<div class="date">';
            $content .=$row['Date'];
            $content .='</div>';
        $content .= '</div>';
        $filename ="footer.html";
        @unlink($filename);
        $handle = fopen("footer.html", 'w+');
        echo $row['Title'].'<br>';
        if ($handle) 
        {
            if (!fwrite($handle, $content))
            die("cant' write");
        }
}
    ?>

您需要将初始$content变量和文件写入部分置于for循环外部

$content='';
而($row=mysql\u fetch\u数组($retval,mysql\u ASSOC))
{
//echo“”;print_r($row);echo“”;
$content = '';
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
        //echo "<pre>"; print_r($row); echo "</pre>";
        $content .= '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
        <div class="block">';
            $content .='<a href="="">'.$row['Title'].'</a>';
            $content .='<div class="date">';
            $content .=$row['Date'];
            $content .='</div>';
        $content .= '</div>';

}
$filename ="footer.html";
@unlink($filename);
$handle = fopen("footer.html", 'w+');
echo $row['Title'].'<br>';
if ($handle) 
{
    if (!fwrite($handle, $content))
    die("cant' write");
}
$content.=' '; $content.=''; $content.=''; $content.=$row['Date']; $content.=''; $content.=''; } $filename=“footer.html”; @取消链接($filename); $handle=fopen(“footer.html”,“w+”); echo$row['Title']。
; 如果($handle) { 如果(!fwrite($handle,$content)) 死(“不会写”); }
您每次都会覆盖$content,因为它在循环中,我解决了它,$filename=“footer.html”;$handle=fopen(“footer.html”,“w+”);必须高于while;)