MySQL to RSS feed(XML)-Used in Google Product Search

Hello Friends !!

In my last post, I explain you how to export data from MySQL to tab formatted TXT file.

I think you all aware about Google Product Search.

In Google Product Search, which is used for search any product around the world. Currently many shopping sites stores their products in Google Product Search.

For store data in google product search, Google supports txt and xml (RSS) file.

Now I make one another script which used to export data from MySQL to RSS (XML). Actually google product also preffer RSS feed.

For this you have to create one datafeed.xml and save this file where PHP script file is stored. Here I give you code :
Click Here for more details :

<?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>hootofloot</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”;
}

}

?>

I hope this post become helpful to you my friends. If you have any problem or any query then feel free to comment me.

Thanks.

  • Pingback: MySQL to RSS feed(XML)-Used in Google Product Search | Tutz - Best Tutorials()

  • http://www.intecca.com/ product feed

    Thank you for the gift.I really looking for script to convert mysql to rss feed.

  • http://www.intecca.com product feed

    Thank you for the gift.I really looking for script to convert mysql to rss feed.

  • admin

    Its my goodness that you like my post. I will always help you guys.

  • admin

    Its my goodness that you like my post. I will always help you guys.

  • Pingback: MySQL to RSS feed(XML)-Used in Google Product Search | php genious » KHMER855.COM()

  • http://www.websitedesignmagazine.com/ Joe

    thanks for posting this, it was a huge time saver for building an xml google product feed. of course I had to make some modifications and additions, including adding more fields, but this was a great framework to start with. Thanks again!

  • http://www.websitedesignmagazine.com Joe

    thanks for posting this, it was a huge time saver for building an xml google product feed. of course I had to make some modifications and additions, including adding more fields, but this was a great framework to start with. Thanks again!

  • Webster

    Thank you for this code, it was the break through I was looking for… Keep up the gr8 work! 🙂

  • lissy

    Hi, I am not too familiar with this, I hope it's not too much to ask for clearer step to do this. Where do I upload this file to? How do I extract the file for google product?

  • Timir

    Hello,

    You have to open your google merchant account in google webmaster tools. In that there is one option from which you can upload this file. Google take some time to verify your products.

  • Rajendra

    Really nice script…I need to modify it as per my requirement, but good for initial startup

  • Msodhi2002

    Great Job, good for initial start up

  • Test

     tula kahich yet nahi