Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

[PHP] Output DB as external XML file

  • 20-04-2006 12:44PM
    #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: 261 ✭✭bishopLEN


    Hi,
    I have never done PHP just though I might try it since it's late and all

    This is just from reading a tutorial, hope it helps and there's not too many errors.
    [PHP]
    $file= fopen("rss.xml" , "w") or die("can't open file");
    $xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
    fwrite($file, $xml_output);
    $xml_output .= "<entries>\n";
    fwrite($file, $xml_output);

    for ($x = 0; $x < mysql_num_rows($result) ; $x++){
    $row = mysql_fetch_assoc($result);
    $xml_output .= "\t<player>\n";
    fwrite($file, $xml_output);
    $xml_output .= "\t\t<name>" . $row . "</name>\n";
    fwrite($file, $xml_output);
    $xml_output .= "\t\t<pos>" . $row . "</pos>\n";
    fwrite($file, $xml_output);
    $xml_output .= "\t\t<dob>" . $row . "</dob>\n";
    fwrite($file, $xml_output);
    $xml_output .= "\t\t<apps>" . $row . "</apps>\n";
    fwrite($file, $xml_output);
    // 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";
    fwrite($file, $xml_output);
    $xml_output .= "\t</player>\n";
    fwrite($file, $xml_output);
    }
    $xml_output .= "</entries>";
    fwrite($file, $xml_output);
    fclose($file);
    [/PHP]


Advertisement