with fopen() you can open a file for reading/writing with fwrite() you can write the query into yur txt-file... use a while() or foreach() to read through the whole query_result-array