PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Dear all, please excuse my ignorance I am a newbie to php and have a slight issue. I have a property database that I wish to print the output exactly as it appears in the echo to a file, right now I get exactly what I need in the following code using the echo, but I cant seem to declare the output as $data to use to save as a file.
here is my code that I am using to echo. I want to be able to use something like the following to write the output to a text file.
but its not working.
Any help would be really appreciated.
Thanks in advance.
Steve
you have missed the first parameter of fwrite function, it should be resource handle $file, also use "\n" for new line instead of "<br>" while writing in text file, also remove echo.
thanks for your super quick reply, I have tried that but now none of the data is showing and there is only one record in the text file instead of hundreds. ??
You need to open the file in append mode instead of write mode I missed that to mention. Also you can use variable for data which you can echo and write to file. Use below given code
You're opening the file repeatedly while iterating over your query results, but you're never closing it. It would make more sense to append every result set to a variable, then open the file, write, and immediately close at the end.
Thanks for your help, but I am not sure where I am meant to place my database fields in this code also is there meant to be anything inside the following line of code.
$output = '';
<?php
// Really, no. Use PDO.
$con = mysql_connect('IP','user','pass');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database', $con);
$result = mysql_query("SELECT * FROM table where icon1 = 'Active'");
$output = ''; // Just initializing the variable. This is intentionally empty.
while ($row = mysql_fetch_array($result)) {
// Now we add data to the variable
$output .= "#DATA#<BR>_11980{$row['ATP_Reference_No']}^{$row['NearestTown']}^Algarve^PT^{$row['Description']}<br><br>";
}
$file = fopen("test.txt", "a+");
fwrite($file, $output); // No results + uninitialized variable = error here
fclose($file);
this is my exact code. I added the echo to see if anything was being shown its empty no output. The txt file is being created but has a 0 size after the script is run.
<?php
$con = mysql_connect("IP","USER","Pass");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('DB', $con);
$result = mysql_query("SELECT * FROM table where icon1 = 'Active'");
$output = ''; // Just initializing the variable. This is intentionally empty.
while ($row = mysql_fetch_array($result)) {
// Now we add data to the variable
$output .= "#DATA#<BR>_11980{$row['ATP_Reference_No']}^{$row['NearestTown']}^Algarve^PT^{$row['Description']}<br><br>";
}
echo $output;
$file = fopen("test.txt", "a+");
fwrite($file, $output); // No results + uninitialized variable = error here
fclose($file);
?>
The lines should be equivalent. You had smart quotes in there, which can cause problems, so I replaced those with regular quotes and got rid of all the concatenation as it wasn't necessary. Replace that line with yours, replacing echo with $output .= and see if that solves your problem.
Nope that didn't help either, no output on the echo $output when I tried that.
I tried stripping it down to just one database field like below but still nothing.
also if is enter test in this line $output = 'test'; // Just initializing the variable. This is intentionally empty. the word test appears in the text file but no data.
$output = ''; // Just initializing the variable. This is intentionally empty.
while ($row = mysql_fetch_array($result)) {
// Now we add data to the variable
$output .= "{$row['ATP_Reference_No']}";
}
echo $output;
$file = fopen("test.txt", "a+");
fwrite($file, $output); // No results + uninitialized variable = error here
fclose($file);
Steve
Last edited by scoobysteve on Wed Jul 30, 2014 9:32 am, edited 1 time in total.