-1

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());
}

?> 
Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
user3818265
  • 81
  • 1
  • 7

1 Answers1

0

You're not concatenating your query correctly.

You need to add . after every single quote in your $query = $con->prepare line. You also need to add escaped ' around every variable for SQL Server to be able to read your values correctly.

For example: SELECT * from [feedtable] where title = \'' . $item->title . '\' AND link = \''. $item->link. '\' AND ...

And so on and so forth for the rest of that line.

Baher Ramzy
  • 171
  • 6
  • i did what you asked `$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.'\'');`but im gettimg error from sql `SQLSTATE[IMSSP]: An error occurred substituting the named parameters.` – user3818265 Mar 03 '15 at 18:18