Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

[PHP] Output DB as external XML file

  • 20-04-2006 11:45am
    #1
    Registered Users, Registered Users 2 Posts: 3,514 ✭✭✭


    Hi,

    The following code runs a query on a database and outputs the results as XML within its self. I have attempted to get the php page to create a file called rss.xml but have failled. I have tried using $file= fopen("rss.xml" , "w"); but my knowlege of PHP wouldn't be the best or anyway near to the level of being able to modify the below page to do as i wish, would someone be able to take a peep at the page please?

    [PHP]<?
    header("Content-type: text/xml");
    $host = "***********";
    $user = "***********";
    $pass = "***********";
    $database = "***********";
    $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
    mysql_select_db($database, $linkID) or die("Could not find database.");
    $query = "SELECT * FROM mynews ";
    $result = mysql_query($query, $linkID) or die("Data not found.");


    $xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
    $xml_output .= "<entries>\n";
    for ($x = 0; $x < mysql_num_rows($result) ; $x++){
    $row = mysql_fetch_assoc($result);
    $xml_output .= "\t<player>\n";
    $xml_output .= "\t\t<name>" . $row . "</name>\n";
    $xml_output .= "\t\t<pos>" . $row . "</pos>\n";
    $xml_output .= "\t\t<dob>" . $row . "</dob>\n";
    $xml_output .= "\t\t<apps>" . $row . "</apps>\n";
    // Escaping illegal charcters
    $row = str_replace("&", "&", $row);
    $row = str_replace("<", "&lt", $row);
    $row = str_replace(">", "&gt", $row);
    $row = str_replace("\"", "&quote", $row);
    $xml_output .= "\t\t<text>" . $row . "</text>\n";
    $xml_output .= "\t</player>\n";
    }
    $xml_output .= "</entries>";
    print $xml_output;
    ?> [/PHP]


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    where you have
    print $xml_output;
    

    try using the following
    $filename = 'rss.xml';
    if (is_writable($filename)) {
    
       // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence
       // that's where $somecontent will go when we fwrite() it.
       if (!$handle = fopen($filename, 'w')) {
             echo "Cannot open file ($filename)";
             exit;
       }
    
       // Write $somecontent to our opened file.
       if (fwrite($handle, $xml_output) === FALSE) {
           echo "Cannot write to file ($filename)";
           exit;
       }
      
       echo "Success, wrote data to file ($filename)";
      
       fclose($handle);
    
    } else {
       echo "The file $filename is not writable";
    }
    
    


  • Registered Users, Registered Users 2 Posts: 3,514 ✭✭✭Rollo Tamasi


    hi Pe3nom, i got it working now (kind of)
    it out puts a xml file called cormac.xml but the php file still throws back an xml error within itself which is odd.
    XML Parsing Error: no element found
    Location: http://www.pixelscience.it/test.php
    Line Number 1, Column 2:
    -^

    This si the working php code
    [PHP] $file= fopen('cormac.xml' , 'w');
    $xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
    $xml_output .= "<entries>\n";
    for ($x = 0; $x < mysql_num_rows($result) ; $x++){
    $row = mysql_fetch_assoc($result);
    $xml_output .= "\t<player>\n";
    $xml_output .= "\t\t<name>" . $row . "</name>\n";
    $xml_output .= "\t\t<pos>" . $row . "</pos>\n";
    $xml_output .= "\t\t<dob>" . $row . "</dob>\n";
    $xml_output .= "\t\t<apps>" . $row . "</apps>\n";
    // Escaping illegal charcters
    $row = str_replace("&", "&", $row);
    $row = str_replace("<", "&lt", $row);
    $row = str_replace(">", "&gt", $row);
    $row = str_replace("\"", "&quote", $row);
    $xml_output .= "\t\t<text>" . $row . "</text>\n";
    $xml_output .= "\t</player>\n";

    }
    $xml_output .= "</entries>";

    fputs($file, $xml_output);
    fclose($file);[/PHP]

    i don't suppose you know how i could get it read out <rss version=2> just below the xml tag? i have tried <$xml_output = "<?rss version=\"2.0\" ">\n";
    but it doesn't seem to take it.


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    after the xml tag try this


    $xml_output .= "<?rss version=\"2.0\" ">\n";


  • Registered Users, Registered Users 2 Posts: 3,514 ✭✭✭Rollo Tamasi


    Ph3n0m wrote:
    after the xml tag try this


    $xml_output .= "<?rss version=\"2.0\" ">\n";

    yup! cheers, :)


Advertisement