Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 2.07 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Character encoding error, cannot write valid XML from MySQL via PHP
  2. <?php
  3.  
  4. $database =  'xxxx';
  5. $dbconnect = mysql_pconnect('xxxx', 'xxxx', 'xxxx');
  6. mysql_select_db($database, $dbconnect);
  7.  
  8. $query = "SELECT * FROM the_queue WHERE id LIKE '%'    ORDER BY id DESC LIMIT 25";
  9. $result = mysql_query($query, $dbconnect);
  10.  
  11. while ($line = mysql_fetch_assoc($result))
  12.         {
  13.             $return[] = $line;
  14.         }
  15.  
  16. $now = date("D, d M Y H:i:s T");
  17.  
  18. $output = "<?xml version="1.0" encoding="UTF-8"?>
  19.             <rss version="2.0">
  20.                 <channel>
  21.                     <title>The Queue</title>
  22.                     <link>http://readapp.net</link>
  23.                     <description>A curated reading list.</description>
  24.                     <language>en-us</language>
  25.                     <pubDate>$now</pubDate>
  26.                     <lastBuildDate>$now</lastBuildDate>
  27.             ";
  28.  
  29. foreach ($return as $line)
  30. {
  31.     $output .= "<item><title>".htmlspecialchars($line['title'])."</title>
  32.     <description>".htmlspecialchars($line['description'])."</description>
  33.                     <link>".htmlspecialchars($line['link'])."</link>
  34.                     <pubDate>".htmlspecialchars($line['pubDate'])."</pubDate>
  35.                 </item>";
  36. }
  37. $output .= "</channel></rss>";
  38.  
  39. $fh = fopen('feed.xml', 'w');
  40. fwrite($fh, $output);
  41. ?>
  42.        
  43. <head>
  44.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  45.   ...
  46. </head>
  47.        
  48. $database =  'xxxx';
  49. $dbconnect = mysql_pconnect('xxxx', 'xxxx', 'xxxx');
  50. mysql_select_db($database, $dbconnect);
  51. mysql_query('SET NAMES utf8', $dbconnect);
  52.        
  53. CREATE TABLE `the_queue` (
  54.   ...
  55. ) ... DEFAULT CHARSET=utf8;
  56.        
  57. `description` varchar(255) CHARACTER SET latin1,
  58.        
  59. $output = "<?xml version="1.0" encoding="UTF-8"?>
  60. ...
  61.        
  62. $output = "<?xml version="1.0" encoding="UTF-8"?".">
  63. ...
  64.        
  65. $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.”';
  66.  
  67. echo "<description>" . htmlspecialchars($line['description'], ENT_NOQUOTES, 'UTF-8', false) . "</description>";