Hi, I want to list the results from my database into .txt file, how can i do this in php ? is there any good tutorials ? Order, Check if text file exists (if not die !) If true, overwrite all contents with new results from database. Note: reults being pulled from the database will be in thousands..problem ?
no problems at all. First you need to read from your query all information and create a buffer with all data that has to be writen to a file (variabel) and then use fopen, fputs and fclose to open the file, add content and close it!. That's all.
You'll likely want to skip PHP for this task. There's a good chance your database server has a command available for exporting directly to a file.
Here's a snippet that you can use after you've gotten the data from the database written into a variable: <?php #open the file for writing only, erase contents $fp = fopen('results.txt', 'w'); #write the contents of $results to the file fwrite($fp, $results); #close the file fclose($fp); ?> PHP: I'll assume that the database records are in a variable called $results, but you can change this to whatever you like. This will write the contents of $results to a file called results.txt. Make sure that the file is writable, or that the directory is writable so that it can create the file if it does not exist.
or, if you don't want to use fopen you could do if(file_exists("file.txt")) file_put_contents("whatever"); else die;