I have script which downloading rss feed from the internet and store it in Microsoft SQL database. It's working perfectly, I'm able to download RSS feed and store it in MSSQL database.
But I'm getting error will executing the query:
PHP Parse error: syntax error, unexpected '$item' (T_VARIABLE) in C:\phptest\test7.php on line 37
This error query is supposed to check duplicate feed in the database, but it's not working.
php script
<?php
try {
$con = new PDO("sqlsrv:Server=hadi-tosh;Database=rssfeed", "rssuser", "rssuser");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
$url = "http://www.albaldnews.com/rss.php?cat=24";
$rss = simplexml_load_file($url);
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$source = "اخبارالبلد";
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
$category = $item->category;
$guid = $item->guid;
$enclosure = $item->enclosure[0]['url'];
$img2 = $item->enclosure[0]['url'];
$ch = curl_init ($enclosure);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$img = "$rawdata";
$query = $con->prepare('SELECT * from [feedtable] where title = '$item->title' AND link = '$item->link' AND published_on = '$item->pubDate' AND description = '$item->description' AND category = '$item->category' AND image = '$img' AND imagelink = '$img2' AND source = '$source'');
$query->execute();
$num_rows = $query->fetchColumn();
if($num_rows == 0)
{
$stmt = $con->prepare('insert into [feedtable] (title,link,pubdata,description,category,image,imagelink,source) values (:title,:link,:pubdata,:description,:category,:image,:imagelink,:source)');
$stmt->bindParam(':title',$item->title);
$stmt->bindParam(':link',$item->link);
$stmt->bindParam(':pubdata',$item->pubDate);
$stmt->bindParam(':description',$item->description);
$stmt->bindParam(':category',$item->category);
$stmt->bindParam(':image', $img, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
$stmt->bindParam(':imagelink',$img2);
$stmt->bindParam(':source', $source );
$stmt->execute();
echo "albaldnews feeds added\n";
}
else
{
echo "albaldnews duplicate entry\n";
}
}
}
}
catch (PDOException $e) {
exit("Connection failed: " . $e->getMessage());
}
?>