I am new in php nd want to help in generating rss feed. i have a db with name "test123" having fields Id, Name, Address, Designation and Text. Here is my code for rss feed`
<?php
function connect() {
return new PDO('mysql:host=localhost;dbname=test123', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
$pdo = connect();
// posts *******************************
$sql = 'SELECT * FROM form ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute();
$rs_post = $query->fetchAll();
// The XML structure
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>PACRA Rss Feed</title>';
$data .= '<link>http://www.pacra.com</link>';
$data .= '<description>Pacra Pakistan</description>';
foreach ($rs_post as $row) {
$data .= '<item>';
$data .= '<title>'.$row['Name'].'</title>';
$data .= '<link>'.$row['Address'].'</link>';
$data .= '<description>'.$row['Text'].'</description>';
$data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';
header('Content-Type: application/xml');
echo $data;
?>`
Problem is that when i run that code it show an error message "Opening and Ending Tag mismatch" Here is the image of error Image Link
It might be the header, I use the following in my PHP generated RSS feeds:
header('Content-type: text/xml; charset=UTF-8');
And escape your
<title>
and<description>
withCDATA
.Did you validate your feed using the RSS validator? https://validator.w3.org/feed/
Also my structure is a bit different: