Print to file from php while loop

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!

Moderator: General Moderators

scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Print to file from php while loop

Post 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);
?>
Last edited by Celauran on Wed Jul 30, 2014 6:59 am, edited 1 time in total.
Reason: Please wrap your code in syntax tags.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Print to file from php while loop

Post 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);
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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. ??
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Print to file from php while loop

Post 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);
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Print to file from php while loop

Post 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);
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Print to file from php while loop

Post 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);
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post by scoobysteve »

Celauran thanks I have tried this and the test.txt file is empty ??

I do appreciate your help.
Steve
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Print to file from php while loop

Post by Celauran »

Did you check the content of $output? Is your query returning anything? What about $file? Did it successfully open the file?
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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);

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Print to file from php while loop

Post by Celauran »

If $output is an empty string, then it looks like you aren't getting any results back from your query.
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Print to file from php while loop

Post 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.
scoobysteve
Forum Newbie
Posts: 14
Joined: Wed Jul 30, 2014 5:19 am

Re: Print to file from php while loop

Post 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
Last edited by scoobysteve on Wed Jul 30, 2014 9:32 am, edited 1 time in total.
Post Reply