获取错误将执行查询(T_VARIABLE)

时间:2015-03-03 17:43:38

标签: php mysql

我有从互联网下载rss feed并将其存储在Microsoft SQL数据库中的脚本。 它工作得很好,我可以下载RSS源并将其存储在MSSQL数据库中。

但是我收到错误会执行查询:

  

PHP Parse错误:语法错误,第37行的C:\ phptest \ test7.php中的意外'$ item'(T_VARIABLE)

此错误查询应该检查数据库中的重复提要,但它不起作用。

php脚本

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

?> 

1 个答案:

答案 0 :(得分:0)

您没有正确连接查询。

您需要在.行的每个引号后添加$query = $con->prepare。您还需要在SQL Server的每个变量周围添加转义',以便能够正确读取您的值。

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

依此类推,等等其余部分。

相关问题