Page 1 of 2
Print to file from php while loop
Posted: Wed Jul 30, 2014 5:29 am
by scoobysteve
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
Code: Select all
<?php
$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'");
while($row = mysql_fetch_array($result))
$file = fopen("test.txt","w");
echo fwrite("#DATA#"."<BR>"."_11980".$row['ATP_Reference_No']."^".$row['NearestTown']."^Algarve^PT^".$row['Description']."<br><br>”);
fclose($file);
mysql_close($con);
?>
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 5:41 am
by pbs
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.
Code: Select all
$file = fopen("test.txt","w");
fwrite($file,"#DATA#"."\n"."_11980".$row['ATP_Reference_No']."^".$row['NearestTown']."^Algarve^PT^".$row['Description']."\n\n");
fclose($file);
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 5:50 am
by scoobysteve
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. ??
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 6:02 am
by pbs
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
Code: Select all
$file = fopen("test.txt","a");
$data = "#DATA#"."\n"."_11980".$row['ATP_Reference_No']."^".$row['NearestTown']."^Algarve^PT^".$row['Description']."\n\n";
echo $data."<br>";
fwrite($file,$data);
fclose($file);
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 6:09 am
by scoobysteve
thanks again OK slight improvement the following is in the text file that was created.
#DATA#
_11980^^Algarve^PT^
#DATA#
_11980^^Algarve^PT^
as you can see there is no data from the database fields and also only 2 records.
Thanks for your help so far.
Steve
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 7:03 am
by Celauran
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.
Code: Select all
$output = '';
foreach ($results as $result) {
$output .= "Some info: {$result['foo']}\n";
}
$fh = fopen('/path/to/file', 'a+');
fwrite($fh, $output);
fclose($fh);
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 8:38 am
by scoobysteve
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 = '';
Thanks
Steve
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 8:44 am
by Celauran
Clearer?
Code: Select all
<?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);
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 8:51 am
by scoobysteve
Celauran thanks I have tried this and the test.txt file is empty ??
I do appreciate your help.
Steve
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 8:52 am
by Celauran
Did you check the content of $output? Is your query returning anything? What about $file? Did it successfully open the file?
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 8:58 am
by scoobysteve
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.
Code: Select all
<?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);
?>
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 9:01 am
by Celauran
If $output is an empty string, then it looks like you aren't getting any results back from your query.
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 9:05 am
by scoobysteve
does that mean the line
$output .= "#DATA#<BR>_11980{$row['ATP_Reference_No']}^{$row['NearestTown']}^Algarve^PT^{$row['Description']}<br><br>";
is wrong, I noticed you stripped the comments and .$row out of the original that looked like.
echo "#DATA#"."<BR>"."_11980".$row['ATP_Reference_No']."^".$row['NearestTown']."^Algarve^PT^".$row['Description']."<br><br>";
is that right the last line works in echo
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 9:11 am
by Celauran
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.
Re: Print to file from php while loop
Posted: Wed Jul 30, 2014 9:14 am
by scoobysteve
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.
Code: Select all
$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