$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);
}
}
Edit: changed flag from w to a.
Another edit: if you want to APPEND to the file, leave the fopen() flag set to 'a'. If you want to overwrite the existing contents, change the flag to 'w'.
Final edit: added two versions with explanation