Jump to content

XML and PHP - pretty basic stuff


Recommended Posts

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

Link to comment
Share on other sites

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.

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.