- Character encoding error, cannot write valid XML from MySQL via PHP
- <?php
- $database = 'xxxx';
- $dbconnect = mysql_pconnect('xxxx', 'xxxx', 'xxxx');
- mysql_select_db($database, $dbconnect);
- $query = "SELECT * FROM the_queue WHERE id LIKE '%' ORDER BY id DESC LIMIT 25";
- $result = mysql_query($query, $dbconnect);
- while ($line = mysql_fetch_assoc($result))
- {
- $return[] = $line;
- }
- $now = date("D, d M Y H:i:s T");
- $output = "<?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>The Queue</title>
- <link>http://readapp.net</link>
- <description>A curated reading list.</description>
- <language>en-us</language>
- <pubDate>$now</pubDate>
- <lastBuildDate>$now</lastBuildDate>
- ";
- foreach ($return as $line)
- {
- $output .= "<item><title>".htmlspecialchars($line['title'])."</title>
- <description>".htmlspecialchars($line['description'])."</description>
- <link>".htmlspecialchars($line['link'])."</link>
- <pubDate>".htmlspecialchars($line['pubDate'])."</pubDate>
- </item>";
- }
- $output .= "</channel></rss>";
- $fh = fopen('feed.xml', 'w');
- fwrite($fh, $output);
- ?>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- ...
- </head>
- $database = 'xxxx';
- $dbconnect = mysql_pconnect('xxxx', 'xxxx', 'xxxx');
- mysql_select_db($database, $dbconnect);
- mysql_query('SET NAMES utf8', $dbconnect);
- CREATE TABLE `the_queue` (
- ...
- ) ... DEFAULT CHARSET=utf8;
- `description` varchar(255) CHARACTER SET latin1,
- $output = "<?xml version="1.0" encoding="UTF-8"?>
- ...
- $output = "<?xml version="1.0" encoding="UTF-8"?".">
- ...
- $line['description'] = '"Dave, stop. Stop, will you? Stop, Dave. Will you stop, Dave?” ... “Dave, my mind is going,” HAL says, forlornly. “I can feel it. I can feel it.”';
- echo "<description>" . htmlspecialchars($line['description'], ENT_NOQUOTES, 'UTF-8', false) . "</description>";