Question

<?php
    header('Content-type: text/xml; charset=utf-8');
    DEFINE ('DB_USER', 'root');   
    DEFINE ('DB_PASSWORD', '');   
    DEFINE ('DB_HOST', 'localhost');   
    DEFINE ('DB_NAME', 'rss'); 
    $output  = '<?xml version="1.0" encoding="UTF-8"?>';
    $output .= '<rss version="2.0">';
    $output .= '<channel>';
    $output .= '<title>Feed Demo</title>';
    $output .= '<description>News Feed from MySQL test.</description>';
    $output .= '<language>de</language>';

    $connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('-');
    mysql_select_db(DB_NAME) or die ('-');

    $query = "SELECT * FROM entry ORDER BY date DESC";
    $result = mysql_query($query) or die ('-');

    while($row = mysql_fetch_array($result)) {
        extract($row);
        $output .= '<item>';
            $output .= '<title>' . $title . '</title>';
            $output .= '<description>' . $description . '</description>';
            $output .= '<link>' . $link . '</link>';
            $output .= '<pubDate>' . date("d. F Y, H:i", strtotime($date)) . '</pubDate>';
        $output .= '</item> ';
    } 

   $output .= '</channel>';
   $output .= '</rss>';

    echo($output);
?>

Hello! I'm using the code above to generate a rss-feed from a MySql database. It work's very well, until I add special characters to the entries e.g. :,/,ä,ö,ü,ß,?," and so on. I get the message

This page contains the following errors:

error on line 1 at column 377: Encoding error Everything before the row with the special characters is displayed properly.

Was it helpful?

Solution

You should use utf8_encode() to encode all your values.

something like:

while($row = mysql_fetch_array($result)) {
    extract($row);
    $output .= '<item>';
        $output .= '<title>' . utf8_encode($title) . '</title>';
        $output .= '<description>' . utf8_encode($description) . '</description>';
        $output .= '<link>' . $link . '</link>';
        $output .= '<pubDate>' . date("d. F Y, H:i", strtotime($date)) . '</pubDate>';
    $output .= '</item> ';
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top