首先建立一个conn.php的文件用来链接数据库
<?php<BR> $link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error()); <BR> mysql_query("set names utf8"); <BR> mysql_select_db("my_database") or die("Could not select database");<BR>?><BR>
php 批量生成html
<?php<BR> require_once(“conn.php”);<BR> $query = "SELECT id,title,introduce FROM my_table";<BR> $result = mysql_query($query) or die("Query failed : " . mysql_error()); <BR> /* 生成 HTML 结果 */<BR> while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {<br><br> $id=$row['id'];<BR> $title=$row['title'];<BR> $introduce=$row['introduce'];<BR> $path="html/$id.html";<BR> $fp=fopen("template.html","r"); //只读打开模板<BR> $str=fread($fp,filesize("template.html"));//读取模板中内容<BR> $str=str_replace("{title}",$title,$str);<BR> $str=str_replace("{introduce}",$introduce,$str);//替换内容<BR> fclose($fp);<BR> $handle=fopen($path,"w"); //写入方式打开新闻路径<BR> fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件<BR> fclose($handle);<BR> //echo "生成成功"."<br>";<BR> }<BR> /* 释放资源 */<BR> mysql_free_result($result);<BR> mysql_close($link);<BR>?> <BR>
template.html文件内容:
<BR><BR><BR><BR><meta http-equiv<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b><strong>搞gaodaima代码</strong>="Content-Type" content="text/html; charset=utf-8" /><BR><title>{title}</title><BR><BR><body><BR>{introduce}<BR><BR><BR>
php 批量生成txt
<BR><?php<BR> require_once(“conn.php”);<BR> $query = "SELECT kid,title,introduce FROM pro_courses";<BR> $result = mysql_query($query) or die("Query failed : " . mysql_error()); <BR> /* 生成 txt 结果 */<BR> while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {<br><br> $id=$row['id'];<BR> $title=$row['title'];<BR> $introduce=$row['introduce'];<BR> $path="html/$id.txt";<BR> $handle=fopen($path,"w"); //写入方式打开新闻路径<BR> fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件<BR> fclose($handle);<BR> }<BR> /* 释放资源 */<BR> mysql_free_result($result);<BR> mysql_close($link);<BR>?> <BR>