Jump to content

Retrieve data error...plz help


cjay

Recommended Posts

hello ppl!

 

i have a xml file and i refresh it with a php file...

into the php file i have a query which i retrieve my data from a sql db...the output result is n't the right...because it "write" to the xml file only the last record from db...look what i mean:

 

php file:

<?php
@include("xconf.php");

$query  = "SELECT * FROM images";
$result = mysql_query($query);

while($oz = mysql_fetch_array($result))
{

$myFile = "data.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?xml version=\"1.0\" encoding=\"ISO-8859-7\" ?>\n\n<gallery>\n\n<photos>\n";
fwrite($fh, $stringData);
$stringData = "<photo>\n<thumb src=\"$oz[mikro]\" />\n<img src=\"$oz[s]\" />\n<caption text=\"$oz[title]\" />\n<desc> <![CDATA[$oz[per]]]> </desc>\n</photo>\n\n";
fwrite($fh, $stringData);
$stringData = "</photos>\n\n";
fwrite($fh, $stringData);
$stringData = "</gallery>\n";
fwrite($fh, $stringData);

fclose($fh);

}
?> 

 

and the data.xml:

 

<?xml version="1.0" encoding="ISO-8859-7" ?>

<gallery>

<photos>
<photo>
<thumb src="http://www.talkphp.com/images/thumb_Free_web_development_icon_set.jpg" />
<img src="http://www.talkphp.com/images/Free_web_development_icon_set.jpg" />
<caption text="yt4" />
<desc> <![CDATA[er]]> </desc>
</photo>

</photos>

</gallery>

 

how can i correct the code? i want the output result appear all the db records...:(

 

thank you

friendly

Link to comment
Share on other sites

$fh = fopen($myFile, 'w') or die("can't open file");

 

 

You're reopening the file handle every time the loop runs.

 

 

Just so happens that opening for writing (w) truncates the file if it exists.  So you're overwriting the file every time you reopen it.

 

 

(In other words, put the $fh = .... outside of the while loop.  Also, put the fclose outside of the loop.)

 

 

And an unrelated suggestion as to coding style:

 

 

@include("xconf.php");

 

 

Shouldn't have the @.  Error suppression is typically bad (unless for some weird reason it's ok if an error occurs).  If you're worried about users seeing the error in a live environment, set display_errors to 0.  Error suppression makes for slower code and confusion debugging if something does go wrong.  Also, technically include("xconf.php"); could be (and some would say should be) include "xconf.php"; since include is a language construct, not a function (although it really doesn't make much of a difference in this situation).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.