Madmoggie Posted November 8, 2011 Share Posted November 8, 2011 Hello I am trying to build a script that gets data from a database and puts it into an XML file. I am getting the data ok but i can't get it to save as a file.... my code is: <?php // set server access variables include ("connection.php"); // create query $query = "SELECT * FROM players"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes //Top of xml file $_xml = '<?xml version="1.0"?>'; $_xml .="<players>"; while($row = mysql_fetch_array($result)) { $_xml .="<player>"; $_xml .="<player_name>".$row['name']."</player_name>"; $_xml .="<player_email>".$row['email']."</player_email>"; $_xml .="<player_team>".$row['team']."</player_team>"; $_xml .="</player>"; } $_xml .="</players>"; //Output the xml string print $_xml; /* Could also write to a file at this point file_put_contents("somedir/some.xml", $_xml); Need to give 'somedir' write permissions */ } else { // no // print status message echo "No rows found!"; } ?> I know at the bottom it says /* Could also write to a file at this point file_put_contents("somedir/some.xml", $_xml); But i don't know what to do with it. i moved it instead of the print function and tried to put - file_put_contents("/IITA/fingerscrossed.xml", $_xml); but no file is produced. do i need to create the file 'fingerscrossed.xml' first or should it happen automatically Thanks for any help you can give Quote Link to comment https://forums.phpfreaks.com/topic/250701-xml-and-php-pretty-basic-stuff/ Share on other sites More sharing options...
Madmoggie Posted November 8, 2011 Author Share Posted November 8, 2011 I've managed to solve it. If anyone has the same problem this is the code i used: //Output the xml string $filenamepath .= "fingerscrossed.xml"; $fp = fopen($filenamepath,'w'); $write = fwrite($fp,$_xml); Quote Link to comment https://forums.phpfreaks.com/topic/250701-xml-and-php-pretty-basic-stuff/#findComment-1286256 Share on other sites More sharing options...
Adam Posted November 9, 2011 Share Posted November 9, 2011 That will work while $filenamepath is a file within the same directory or in a path that already exists. The problem before was that your path didn't exist, which you would need to use mkdir to create first. Also as a heads up "/IITA/fingerscrossed.xml" - if you're working on a Windows machine this will never exist. The file path is not based on the HTTP path, it's based on the internal file path. Quote Link to comment https://forums.phpfreaks.com/topic/250701-xml-and-php-pretty-basic-stuff/#findComment-1286680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.