How To Generate MySQL To RSS (XML) File Used In Google Product Search

Hi Friends, Some days ago I got one requirement for my one client to make RSS or XML file from mysql table to submit into Google Product Search. Google Product Search helps shoppers find and buy products across the web. As a seller, you can submit your products to Google Product Search, allowing shoppers to quickly and easily find your site. So Google Product Search is very useful thing for people who have website of selling online products. Google Product Search supports .txt and .xml (RSS) file. Im my last article I already explained how we can generate MySQL Table To Tab Formatted TXT Datafeed.

For this you have to create one datafeed.xml and save this file where PHP script file is stored.So below is the script by which you can Generate MySQL To RSS (XML) File Used In Google Product Search:

<?php $conn = mysql_connect('hostname', 'username', 'password') or die ('Error connecting to mysql');
mysql_select_db('DB name', $conn) or die ('Error connecting to database');?>
<?php function getXML($sql="Default Query")
{
$results = mysql_query($sql);

$count = 0;

$data = array();

while ( $row = mysql_fetch_array($results))

{

$result[$count] = $row;

$count++;

}

$columns=";

for($i=0;$i < count($result);$i++)
{

$columns.='<item>';

$columns.='<title>';
$columns.=$result[$i]['name'];
$columns.='</title>';

$columns.='<link>';
$columns.= "http://www.example.com/".$result[$i]['link'];
$columns.='</link>';

$columns.='<description>';
$columns.=$result[$i]['Description'];
$columns.='</description>';

$columns.='<g:image_link>';
$columns.="http://www.example.com/".$result[$i]['image'];
$columns.='</g:image_link>';

$columns.='<g:price>';
$columns.=$result[$i]['Price'];
$columns.='</g:price>';

$columns.='<g:condition>';
$columns.='new';
$columns.='</g:condition>';

$columns.='<g:id>';
$columns.=$result[$i]['id'];
$columns.='</g:id>';

$columns.='</item>';

}
$content='<?xml version="1.0″?>
<rss version="2.0″ xmlns:g="http://base.google.com/ns/1.0″>';
  $content.='
  <channel>';
    $content.='
    <title>Example</title>
    ';
    $content.='
    <link>
    http://www.example.com
    </link>
    ';
    $content.='
    <description>Example</description>
    ';
    $content.=$columns;
    $content.='</channel>
  ';
  $content.='</rss>
';

$res = update_xml_file($content);

}
getXML("SELECT id,name,description,price,link,image FROM tablename");

function update_xml_file($content)
{
global $HTTP_POST_VARS;
$filename = 'datafeed.xml';

if (is_writable($filename))
{
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $content) == FALSE) {
$action_msg = "ER";
}

$action_msg = "SC";
fclose($handle);
$handle=fopen($filename,"r");
}
else
{
echo "The file $filename is not writable";
}

}

?>

If you are having any problems in this script then you can contact me or post problem in comment.

Subscribe to PHP Freelancer

Enter your email address: