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.

You can leave a response, or trackback from your own site.

View Comments to “MySQL to RSS feed(XML)-Used in Google Product Search”

  1. abcphp.com says:

    MySQL to RSS feed(XML)-Used in Google Product Search | php genious…

    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….

  2. [...] post:  MySQL to RSS feed(XML)-Used in Google Product Search By: Admin Date: January 20, 2010 3:33 pm Categories: Object, code, content, ee, free, how to, [...]

  3. product feed says:

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

  4. admin says:

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

  5. [...] Visit link: MySQL to RSS feed(XML)-Used in Google Product Search | php genious [...]

  6. Joe says:

    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!

  7. Webster says:

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

  8. lissy says:

    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?

  9. Timir says:

    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.

Leave a Reply

blog comments powered by Disqus
Subscribe to RSS Feed Follow me on Twitter!
Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Thank you for using IGIT Tweet Share Button, a plugin by HackingEthics