I am guessing that you want your text file to end up something like this example :-
Code:
richard,sheep,cats,dogs,food
mark,clouds,cups,monitor,radio
john,fish,orange,bubbles,telephone
Firstly, grab the data from your database and stuff it into an array.
Then using a while loop, go through all the elements (rows) in that array, writing to your text file as you go.
You can "implode" an array by using the PHP command
implode
And then write the line(s) to your text file using PHP command
file_put_contents
So let's say our database is like this:-
Table : "likes"
col1 | col2 | col3 | col4 | col5
-----------------------------------
richard | sheep | dog | cat | food
mark | clouds | cups | monitor | radio
john | fish | orange | bubbles | telephone
Firstly, we get all the data in there into an array, this example will use the "mysql" library but I wouldn't recommend using it in production as it's now old and you should use mysqli or PDO ...
PHP Code:
$sql = "SELECT * FROM `likes`;";
$result = mysql_query($sql);
The next thing we check to ensure there were some records returned and if not, stop the script (you can of course, use error handling but for this example we're going to kill it, dead ...
PHP Code:
if (!$result) { die("No results"); }
So next we know we have data in the $result variable and we need to shove that into an array ... at the same time, we're going to loop through the data and create a single line which we can use later to write into our .TXT file ...
PHP Code:
while ($row = mysql_fetch_assoc($result)) {
$data[] = implode($row , ",");
}
At this point, we have an array called $data which has (in our case) 3 "rows". Example:-
Code:
Array
(
[0] => richard,sheep,cats,dogs,food
[1] => mark,clouds,cups,monitor,radio
[2] => john,fish,orange,bubbles,telephone
)
So now it's a case of writing these lines to a text file ...
PHP Code:
$file = "likes.csv";
foreach ($data as $key => $value) {
$current = file_get_contents($file);
$current .= "{$value}\n";
file_put_contents($file, $current);
}
"likes.csv" now has a comma separated list of records from your database.