<?php
$mysql="localhost";
$dbname="root";
$dbpwd="";
$db="news";
$conn=@mysql_connect($mysql,$dbname,$dbpwd) or die("连接数据库失败!");
@mysql_select_db($db,$conn) or die("选择表1失败!");
mysql_query("set names 'gb2312' ");
$query=mysql_query("SELECT ask.id, ask.title, askanswer.content from ask, askanswer");
while($row=mysql_fetch_array($query))
{
$id=$row[0];
$title=$row[1];
$content1=$row[2];
$path="html/";
$path=$path.$id.".html";
$fp=fopen("tmp.htm","r"); //只读打开模板
$str=fread($fp,filesize("tmp.htm"));//读取模板中内容
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content1}",$content1,$str);//替换内容
fclose($fp);
$handle=fopen($path,"w"); //写入方式打开新闻路径
fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
echo "生成成功"."<br>";
}
?>
这个代码为什么执行后会如图1:
图1
(图1)
生成的两个HTML文件的标题是对的,但是内容却不一样:如图2,图3
图2
图2
图3
图3
图3
file_get_contents
file_put_contents