$result = mysql_query("SELECT code FROM topic_master");
// if you want to APPEND the data in code.txt, use this
// however, using that particular SQL query the same data will be written over and over because there's nothing specifying a parameter change
while ($row = mysql_fetch_array($result)) {
$x1 = $row['code'];
write_file('code.txt',$x1,'a');
}
// if you want to OVERWRITE the data in code.txt use this
while ($row = mysql_fetch_array($result)) {
$x1 .= $row['code'];
}
write_file('code.txt',$x1,'w');
// function to write content to a file (with error checking)
function write_file($filename,$content,$flag) {
if(is_writable($filename)) {
if(!$handle = fopen($filename, $flag)) {
// die, or (preferably) write to error handler function
die('error');
}
if(fwrite($handle, $content) === FALSE) {
// die, or (preferably) write to error handler function
die('error');
}
fclose($handle);
}
}
编辑:将标志从 w 更改为 a。
另一个编辑:如果要附加到文件,请将 fopen() 标志设置为“a”。如果要覆盖现有内容,请将标志更改为“w”。
最终编辑:添加了两个带有解释的版本