Page 1 of 1

how to write displayed db data into a file

Posted: Tue Jan 03, 2012 4:49 pm
by Tokunbo
hello sirs,

Kindly I need some help. I can connect to and display my data properly from the DB. I want to write the displayed data to a text file. I cant figure out where to put things, considering the while statement in my code.

Code: Select all

$query="SELECT * FROM mytable";
$result=mysql_query($query);
$num=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>
                <td>".$row['field1']."</td>
                <td>".$row['field2']."</td>
                <td>".$row['field3']."</td>
        </tr>";
}
?>
I have tried this after scouring through the net:

Code: Select all

<?php
$fp = fopen($filename, "w");
$query="SELECT * FROM mytable";
$result=mysql_query($query);
	$num=mysql_num_rows($result);

while ($row = mysql_fetch_assoc($result)) {
  echo "<tr>
                <td>".$row['field1']."</td>
                <td>".$row['field2']."</td>
                <td>".$row['field3']."</td>
        </tr>";

flose ($fp);

and I get an error sayiing "an expected $end"......somewhere

Re: how to write displayed db data into a file

Posted: Tue Jan 03, 2012 7:34 pm
by twinedev
A few issues.

1. The "unexpected $end" is because you opened a set or braces after the while statement, but never close it.
2. You are calling flose($fp); and it should be fclose($fp);
3. You need another step to actually write what you want to the file: fwrite($fp,"Stuff to write, you have to specify newlines!\n");

-Greg

Re: how to write displayed db data into a file

Posted: Wed Jan 04, 2012 4:43 am
by Tokunbo
thanks Twinedev,

thanks for your pointers. I have corrected my code accordingly. But with regards to a code like this:

Code: Select all

<?php 
 $File = "YourFile.txt"; 
 $Handle = fopen($File, 'w');
 $Data = "Jane Doe\n"; 
 fwrite($Handle, $Data); 
 print "Data Written"; 
 fclose($Handle); 
 ?>
I understand $Data is supposed to be the output from my DB which shows up with:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
  echo "<tr>
                <td>".$row['field1']."</td>
                <td>".$row['field2']."</td>
                <td>".$row['field3']."</td>
        </tr>";
so my problem now is, how do I assign the above to a variable in order to fit inside the statement fwrite($Handle, $Data); properly. Ive tried several combinations, to put the above into $Data = " "; but I give getting a syntax error.

Pls advise.

thanks
Tokunbo

Re: how to write displayed db data into a file

Posted: Wed Jan 04, 2012 5:14 am
by twinedev
If you are wanting to contain everything, including the HTML around it, then you do the following:

Code: Select all

$Data = '';
while($row = mysql_fetch_assoc($result)) {
    $Data .= '<tr><td>'.htmlspecialchars($row['field1']).'</td>';
    $Data .= '<td>'.htmlspecialchars($row['field2']).'</td>';
    $Data .= '<td>'.htmlspecialchars($row['field3']).'</td></tr>';
}

// Now $Data has all row code

Re: how to write displayed db data into a file

Posted: Wed Jan 04, 2012 5:48 am
by Tokunbo
@Twinedev,

thanks for the help. I have substituted your code for mine and the necessary variables (fields).

Now, no errors are displayed. The yourfile.txt file is generated but its empty. Filesize is 0-KB.

But I have date from my dB displayed and things look ok as far as I can see.

cheers
tokunbo