Jump to content

need help writing XML data to MYSQL with php


simedogz

Recommended Posts

Hello,

 

I am trying to store data from a data feed into a database. The datafeed is an xml object. I am trying to store the price of different books in this database.  to access the different prices in the xml document, I have been using a foreach loop.  What I haven't figured out is how to use a foreach loop to write these values to a database.  Here is the code I have come up with.  However, I get a parse error.  Can anyone tell me what I am doing wrong?  thanks.

 

<?php 
$url='https://product-search.api.cj.com/v2/product-search?website-id=3584291&advertiser-ids=1427863,829234,520129,1551419,904879,1191832,246072,1087150,2020232,2030865,1845757&isbn=0316346624&sort-by=price&serviceable-area=US'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$CJ_KEY ='00bc6addff460bdfb61689a6b153deca753cad4b171931e2bd715099f511dbe1f72944588882ba974f7be7a7490faad8f034209a4c5bb9f510bbcf1a3f3b4a7a77/0090be60307cf51e34d9f78110f4e7500ce442144f785174682826cd3f112b23d8d44136d0f9b5b48ea8089521ec76e295dc50e1b2fa7754e2bb8905c05e152071'; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPHEADER, 
array('Authorization: ' . $CJ_KEY)); 
$curl_results = curl_exec($ch);   
$info = simplexml_load_string($curl_results); 
print_r($info); 


foreach($info->products->product as $cost)
{
echo "<h3>" . $cost->price . "</h3>";
};
// Database entries

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("results", $con);

//code that doesn't work
foreach($info->products->product as $cost2)
{ 
   mysql_query("INSERT INTO search_results (seller_name, price, description, buy_url, date)
   VALUES ('haha', '$cost2->price', 'good', 'www.blah.com', CURDATE())";
};
// end of code that doesn't work

// test that data is getting through to the database
  mysql_query("INSERT INTO search_results (seller_name, price, description, buy_url, date)
VALUES ('blaah', '', 'good', 'www.xyz.com', CURDATE())");
mysql_close($con);
?>

It looks as if you are missing and end parenthsis:

//code that doesn't work
foreach($info->products->product as $cost2)
{ 
   mysql_query("INSERT INTO search_results (seller_name, price, description, buy_url, date)
   VALUES ('haha', '$cost2->price', 'good', 'www.blah.com', CURDATE())";<------HERE
};
// end of code that doesn't work

 

Should be this I think:

//code that doesn't work
foreach($info->products->product as $cost2)
{ 
   mysql_query("INSERT INTO search_results (seller_name, price, description, buy_url, date)
   VALUES ('haha', '$cost2->price', 'good', 'www.blah.com', CURDATE())"";
};
// end of code that doesn't work

 

Good luck,

Handy PHP

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.