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

[PHP] Output DB as external XML file

Options
  • 20-04-2006 12:44pm
    #1
    Registered Users 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