<?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

解决方案 »

  1.   

    file_get_contents
    file_put_contents
    可以用来代替fopen读写文件.至于你的问题,不太明白.
    图二图三的内容是一样的.
      

  2.   

    不太明白你的意思
    不过图片显示:
    标题不一致,内容一致另外你的
    $query=mysql_query("SELECT ask.id, ask.title, askanswer.content FROM ask, askanswer");
    两表没有连接,查询结果将是两表的笛卡尔积
      

  3.   

    于是,结果集中相同的id将多次出现。
    文件重复生成
    因为你的文件名是 $path=$path.$id.".html";
      

  4.   

    echo "--- 开始 ---<br>";
    //连接数据库   
    $rs   =   mysql_query("SELECT did FROM cbb_doc  ");   // where did = '15'
    while(list($did)   =   mysql_fetch_row($rs))   {   
       //"htm/$did.htm"    
       set_time_limit(0); //0为无限制
       $g_htm = $did.".htm" ;
       $f_get = file_get_contents("http://www.xxxxxx.com/index.php?doc-view-".$did);
        $f_put = file_put_contents($g_htm,$f_get);   
        echo "---htm 页面---".$did."---成功生成------<br>";

    echo "--- 结束 ---<br>"; 
      

  5.   

    set_time_limit(0);
    不用放在循环里!
    只要放在当前文件开头就可以了.